def db = [
moduleGroup: 'mysql',
moduleName: 'mysql-connector-java',
moduleVersion: '5.1.18',
driver: "com.mysql.jdbc.Driver",
url: 'jdbc:mysql://localhost:3306/bham',
user: mySqlUser,
password: mySqlPassword
]
configurations {
sql
}
task connect << {
// This is needed to get mySql driver onto the Groovy/Gradle classpath
configurations.sql.each { file ->
println "Adding URL: $file"
gradle.class.classLoader.addURL(file.toURI().toURL())
}
def sql = groovy.sql.Sql.newInstance(db.url, db.user, db.password, db.driver)
sql.execute("actStatusCodeLkp.sql")
String sqlFilePath = "src/main/resources/sqlscripts/actStatusCodeLkp.sql"
String sqlString = new File(sqlFilePath).text
sql.execute(sqlString)
sql.close()
}
actStatusCodeLkp.sql在Groovy /摇篮执行SQL脚本文件
insert into act_status_code (id, code, display_name, code_system_name, code_system) values (1, 'active', 'active', 'ActStatus', '2.16.840.1.113883.5.14');
insert into act_status_code (id, code, display_name, code_system_name, code_system) values (2, 'cancelled', 'cancelled', 'ActStatus', '2.16.840.1.113883.5.14');
insert into act_status_code (id, code, display_name, code_system_name, code_system) values (3, 'aborted', 'aborted', 'ActStatus', '2.16.840.1.113883.5.14');
insert into act_status_code (id, code, display_name, code_system_name, code_system) values (4, 'completed', 'completed', 'ActStatus', '2.16.840.1.113883.5.14');
看来,sql.execute命令不将文件标记化到4个不同的插入语句,并抛出。
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'insert into act_status_code (id, code, display_name, code_system_name, code_syst' at line 2
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
它的工作原理,如果我只是保持一个INSERT语句的文件中。这里干净的工作是什么,没有真正发现任何关于这个在线。 另外,当使用maven时,我可以使用sql-maven-plugin运行相同的sql文件。
+1
无法执行与SQL类多行语句通知让allowMultiQueries 。像这样的事情你可能会获得更多的成功? http://blog.codeborne.com/2012/09/using-dbdeploy-in-gradle.html –
+1
您也可以在文件上调用readLines,该文件会获取一个列表,然后在该列表上调用each()并调用sql。用行executeInsert。这可能有帮助。 –
+0
做了一些研究之后,我想到了MySql,我需要指定'allowMultiQueries:'true''。它的作用像魅力,但如果我在我的文件中有任何评论,例如'--insert into ..',它会以相同的原因失败,我想应该有一种方法来转义注释行吗? –