Executing Sql script file in Groovy/Gradle - mysql

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
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');
It seems that sql.execute command does not tokenize the file into 4 different insert statements and throws.
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)
It works if I just keep one insert statement in the file. What is the clean work around here, did not really find anything regarding this online.
Also, when using maven, I am able to run the same sql file using sql-maven-plugin.

Something like this helped me. Notice getting allowMultiQueries: 'true' in the properties
def props = [user: grailsApplication.config.dataSource.username, password: grailsApplication.config.dataSource.password, allowMultiQueries: 'true'] as Properties
def url = grailsApplication.config.dataSource.url
def driver = grailsApplication.config.dataSource.driverClassName
def sql = Sql.newInstance(url, props, driver)

You could also change your sql to make the statement one query by:
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'),
(2, 'cancelled', 'cancelled', 'ActStatus', '2.16.840.1.113883.5.14'),
(3, 'aborted', 'aborted', 'ActStatus', '2.16.840.1.113883.5.14'),
(4, 'completed', 'completed', 'ActStatus', '2.16.840.1.113883.5.14');

Related

What is syntactically wrong with this ON DUPLICATE KEY UPDATE query?

Update: This issue was resolved in the comments and is awaiting an answer
When executing the following query in PyMySQL, I receive the error ‘1064, u"You have an error in your SQL syntax;’ (full error message below)
INSERT INTO `table_name` (`id`, `colName1`, `colName2`, `colName3`)
VALUES (820, 'string', 5, 'N')
ON DUPLICATE KEY UPDATE
`colName1`=VALUES(`colName1`),
`colName2`=VALUES(`colName2`),
`colName3`=VALUES(`colName3`)
-- Tried with and without ` surrounding column names after ON DUPLICATE KEY UPDATE
Syntax in Python:
sql = "INSERT INTO `table_name` (`id`, `colName1`, `colName2`, `colName3`) VALUES (820, 'string', 5, 'N') ON DUPLICATE KEY UPDATE `colName1`=VALUES(`colName1`),`colName2`=VALUES(`colName2`),`colName3`=VALUES(`colName3`)"
I received this error both when trying to update many rows (as suggested elsewhere, using the format of the accepted answer), and for one row at a time.
As noted in the comments, the stripped down example didn't produce an error (although it was not executed with Python). The following is the actual query. Let me know if other information is needed.
Query string executed in Python:
INSERT INTO `Listings` (`id`, `row_last_updated`, `maxBidCountPreRsvMet`, `maxBidPreRsvMet`, `minBidCountPostRsvMet`, `occupancy_status`, `waitingForHnb`, `ownItNow_price`, `high_bid`, `prop_id`, `hot_property`, `status`, `end_date`, `reserve_met`, `hours`, `backupBidSet`, `listing_type`, `low_bid`, `winning_bid`, `bids`, `days`, `high_bid_updated`, `minutes`, `lowBidIsOpt1`) VALUES (820, '2018-01-28 19:16:02', '5', '234000', None, 'N', None, 0, 234000, u'9007092665103', 'N', 'New', '2018-1-31-22-0', 'N', 7, 0, 'AUCN', 0, 0, 5, 3, '2018-01-28 14:16:02.001906', 44, 0) ON DUPLICATE KEY UPDATE `row_last_updated`=VALUES(`row_last_updated`),`maxBidCountPreRsvMet`=VALUES(`maxBidCountPreRsvMet`),`maxBidPreRsvMet`=VALUES(`maxBidPreRsvMet`),`minBidCountPostRsvMet`=VALUES(`minBidCountPostRsvMet`),`occupancy_status`=VALUES(`occupancy_status`),`waitingForHnb`=VALUES(`waitingForHnb`),`ownItNow_price`=VALUES(`ownItNow_price`),`high_bid`=VALUES(`high_bid`),`prop_id`=VALUES(`prop_id`),`hot_property`=VALUES(`hot_property`),`status`=VALUES(`status`),`end_date`=VALUES(`end_date`),`reserve_met`=VALUES(`reserve_met`),`hours`=VALUES(`hours`),`backupBidSet`=VALUES(`backupBidSet`),`listing_type`=VALUES(`listing_type`),`low_bid`=VALUES(`low_bid`),`winning_bid`=VALUES(`winning_bid`),`bids`=VALUES(`bids`),`days`=VALUES(`days`),`high_bid_updated`=VALUES(`high_bid_updated`),`minutes`=VALUES(`minutes`),`lowBidIsOpt1`=VALUES(`lowBidIsOpt1`)
The full error message:
(1064, u"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 ''9007092665103', 'N', 'New', '2018-1-31-22-0', 'N', 7, 0, 'AUCN', 0, 0, 5, 3, '2' at line 1")
Side notes: Python None values are converted to MySQL null values. The same error occurred when changing None values to strings.

Use of `if( ... = ... )` in mysql?

insert into foo_table (fname, lname, number)
values ('John', 'Doe', if(123 = 456));
For the above MySQL query, can somebody kindly explain what the if(123 = 456) is doing? I currently struggle to see an if statement without a body (i.e. if(condition){ // do something });
The query is syntactically not correct as per mysql version 8, The syntactically correct query is insert into foo_table (fname, lname, number) values ('John', 'Doe', if(123 = 456,1,2)). This will insert 1 if the condition (123 =456) is true, otherwise it will insert 2.

How to set Django to Ignore mysql errors and force mysql to insert a record?

So I am a naive Django developer and want a similar functionality as INSERT IGNORE of mysql in Django. Is there a way to do that? Right now whenever I try to save a record in my database, It throws (1364, Field doesn't have a default value). Should there be any change in Django settings or mysql settings?
You can execute mySQL command in Django.
create_fields = ['f1', 'f2', 'f3']
values = [
(1, 2, 3),
(4, 5, 6),
(5, 3, 8)
]
db_table = self.model._meta.db_table
values_sql = []
values_sql.append( "(%s)" % (','.join([ " %s " for i in range(len(create_fields))]),) ) # correct format
base_sql = "INSERT IGNORE INTO %s (%s) VALUES " % (db_table, ",".join(create_fields))
sql = """%s %s""" % (base_sql, ", ".join(values_sql))
cursor.executemany(sql, values)

Column count doesn't match value count at row

INSERT INTO clnt_reports_01 (r_id, cl_no, cl_no, servi, size, vol,
deliver_point, port_, a_port, road, term, compet, speed,
rcomments, stage, meetrating, username, user_status, kids,
hobbies, comments)
VALUES (1, 123123, "test", "test", "test", "test",
"test", "test", "test", "test", 1, "test", "test",
3, 5, "test", "test", 5, "test", "test");
Getting the error -
Error Code: 1136. Column count doesn't match value count at row
Qoute strings with ' and make sure there is the same number of column in both cases (20):
INSERT INTO clnt_reports_01 (r_id,cl_no,servi,size,vol,deliver_point,port_,a_port,road,term,compet,speed,rcomments,stage,meetrating,username,user_status,kids,hobbies,comments)
VALUES (1, 123123, 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 1, 'test', 'test', 3, 5, 'test', 'test', 5, 'test', 'test');
" is treated as identifier (column name).
Also better use INSERT ... SELECT for readability:
INSERT INTO clnt_reports_01 (
r_id,
cl_no,
servi,
size,
vol,
deliver_point,
port_,
a_port,
road,
term,
compet,
speed,
rcomments,
stage,
meetrating,
username,
user_status,
kids,
hobbies,
comments)
SELECT
1 AS r_id,
123123 AS cl_no,
'test' AS servi,
'test' AS size,
'test' As vol,
'test' AS deliver_point,
'test' AS port_,
'test' AS a_port,
'test' AS road,
'test' AS term,
1 AS compet,
'test' AS speed,
'test' AS rcomments,
3 AS stage,
5 AS meetrating,
'test' AS username,
'test' AS user_status,
5 AS kids,
'test' AS hobbies,
'test' AS comments;
EDIT:
You have specified 21 columns and provided only 20 values so there is a mismatch.
cl_no seems to be repeated twice. Remove that.
You need to use single quotes instead of double for text
INSERT INTO clnt_reports_01 (
r_id,
cl_no,
servi,
size,
vol,
deliver_point,
port_,
a_port,
road,
term,
compet,
speed,
rcomments,
stage,
meetrating,
username,
user_status,
kids,
hobbies,
comments)
VALUES (1,
123123,
'test',
'test',
'test',
'test',
'test',
'test',
'test',
'test',
1,
'test',
'test',
3,
5,
'test',
'test',
5,
'test',
'test');
Insert query to specify 21 columns and passing value 20.
You are providing 20 values for 21 columns, maybe because you've listed the column cl_no twice. Even if you fix the column/value count issue, you'll get this error
Error Code : 1136
Column count doesn't match value count at row 1
well, your column count (21) does not match your value count (20) , you're trying to insert 20 things into 21 columns....
This is given away by the error that says
Error Code: 1136. Column count doesn't match value count at row
For this problem :
Error Code: 1136. Column count doesn't match value count at row
cl_no used twice, remove one this column.
You can use single quotes instead of double quotes.
Such as :
"test" -> 'test'
INSERT INTO fgm_pastor(
matriculePastor,
pastorName,
pastorSurname,
pastorBirthdayDate,
birthdayPlace,
pastorFathername,
pastorMothername,
pastorSexe,
pastorPhone,
pastorEmail,
dateConversion,
workBeforeBibleSchool,
rankProbation,
areaOfCalling,
nberYearArea,
nbreYearDistrict,
martialSituation,
nationality,
pastorAdresse,
photoProfil,
raisonIndispoMissionnaire,
id)
VALUES
(
'matriculetest3',
'nom test',
'prenomtest',
'2013-09-12',
'Dagobert',
'mon pere resr' ,
'ma mere test',
'M',
'phone test',
'pastorEmail test',
'2018-12-28',
'infomaticien',
'rank test',
'area test',
1,
3,
'Single test',
'Cameroun test',
'adresse test',
'phototest'
'RAS',
4
);

Drupal 6 db insert: Strings are getting escaped

Getting driven crazy by this one...
I'm trying to insert a number of rows into a D6 database with a single db_query call. I've collected the rows as a set of strings, and have then collected them into one big string, something like so:
$theData = "(1, 2, 'a'), (3, 4, 'b'), (5, 6, 'c')";
db_query("insert into {table} (int1, int2, str) values %s", $theData);
($theData isn't typed like that in my code; it's the result of the code I've written -- a big string containing sets of values wrapped up in parens.)
When this runs, I get an error like:
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
table (int1, int2, str) values (1,2,\'a\' at line 1 query: insert into
table (int1, int2, str) values (1,2,\'a\'),(3,4,\'n\'),(5,6,\'c\')...
So, db_query or somebody else is escaping the strings before passing the values of to mysql. How do I keep this from happening? I could do individual queries for each set of data, but that's wrong/expensive for all the obvious reasons. Thanks!
$theDatas = array("(1, 2, 'a')", "(3, 4, 'b')", "(5, 6, 'c')");
foreach($theDatas as $data) {
db_query("insert into {table} (int1, int2, str) values %s", $data);
}
But it's not recommend to do that, instead of this you should:
$theDatas = array(array(1, 2, 'a'), array(3, 4, 'b'), array(5, 6, 'c'));
foreach($theDatas as $data) {
db_query("insert into {table} (int1, int2, str) values (%d, %d, '%s')", $data[0], $data[1], $data[2]);
}
Or you can serialize($theData) and put it "text" format field as one value, then use unserialize() for restoring array - this way is recommend if you want only store data (no searching, indexing etc).