groovy附件存mysql_在Groovy /摇篮执行SQL脚本文件

本文档描述了在Groovy中使用Gradle执行包含多个INSERT语句的SQL脚本文件时遇到的问题。Groovy的Sql类无法正确处理多行插入,导致MySQL语法错误。解决方案包括设置'allowMultiQueries: true'或通过读取文件行并逐行执行。同时,文件中的注释可能导致错误,需要寻找适当的方法转义。
摘要由CSDN通过智能技术生成

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 ..',它会以相同的原因失败,我想应该有一种方法来转义注释行吗? –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值