通过文件

时间:2017-08-27 13:13:56

标签: mysql grails

Grails 2.5中,我正在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

虽然当我拿这个文件并在我的数据库上运行它时它会成功插入。

以下是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);

我的数据库是MYSQL 5.7.18,有什么建议吗?

1 个答案:

答案 0 :(得分:1)

看起来像是在输入错误。 可能应该是

sql.execute(sqlStringCountries)

而不是

sql.execute(sqlFilePathCountries)