2017-08-27 27 views
0

Grails 2.5,我做了一些DB數據插入在BootStrap文件,但我總是收到此錯誤:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'countries_data.sql' at line 1通過文件做數據插入Grails的自舉

雖然當我把這個文件,在我的數據庫上運行它,它可以成功插入。

這裏是BootStrap代碼:

def countriesScriptPath = grailsApplication.parentContext.servletContext.getRealPath("/DB_scripts/countries_data.sql") 

    String sqlFilePathCountries = countriesScriptPath 
    String sqlStringCountries = new File(sqlFilePathCountries).text 
    Sql sql = Sql.newInstance(grailsApplication.config.dataSource.url, grailsApplication.config.dataSource.username, 
      grailsApplication.config.dataSource.password, grailsApplication.config.dataSource.driverClassName) 
    sql.execute(sqlFilePathCountries) 

這裏有一些插入語句:

INSERT INTO countries (id,version, iso3166_3, name, phonecode) VALUES (1,0, "AFG", "Afghanistan", 93); // line 1 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (2, 0, 'ALB', 'Albania', 355); 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (3, 0, 'DZA', 'Algeria', 213); 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (4, 0, 'ASM', 'American Samoa', 1684); 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (5, 0, 'AND', 'Andorra', 376); 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (6, 0, 'AGO', 'Angola', 244); 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (7, 0, 'AIA', 'Anguilla', 1264); 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (8, 0, 'ATA', 'Antarctica', 0); 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (9, 0, 'ATG', 'Antigua And Barbuda', 1268); 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (10, 0, 'ARG', 'Argentina', 54); 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (11, 0, 'ARM', 'Armenia', 374); 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (12, 0, 'ABW', 'Aruba', 297); 
INSERT INTO countries (id, version, iso3166_3, name, phonecode) VALUES (13, 0, 'AUS', 'Australia', 61); 

我的DB是MYSQL 5.7.18,有什麼建議?

+0

貌似你試圖傳遞的路徑,文件到sql.execute()。不應該這是sql.execute(sqlStringCountries) –

回答

1

看起來像忘了。 也許,應該是

sql.execute(sqlStringCountries)

,而不是

sql.execute(sqlFilePathCountries)

+0

我也得到了同樣的錯誤,並通過在腳本中更改所有單引號'''雙引號'''來解決它。 – Sherif