在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
,有什麼建議?
貌似你試圖傳遞的路徑,文件到sql.execute()。不應該這是sql.execute(sqlStringCountries) –