node.js - node-mysql multiple statement in one query -
i'm using nodejs 10.26
+ express 3.5
+ node-mysql 2.1.1
+ mysql-server version: 5.6.16
.
i got 4 delete's , want 1 database request, connected delete commands ";"... fails always.
var sql_string = "delete user_tables name = 'testbase';"; sql_string += "delete user_tables_structure parent_table_name = 'testbase';"; sql_string += "delete user_tables_rules parent_table_name = 'testbase';"; sql_string += "delete user_tables_columns parent_table_name = 'testbase';"; connection.query(sql_string, function(err, rows, fields) { if (err) throw err; res.send('true'); });
it throws error:
error: er_parse_error: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'delete user_tables_structure parent_table_name = 'testbase';delete fr' @ line 1
but if paste sql in phpmyadmin successful...
if write in single query's succeed, too.
connection.query("delete user_tables name = 'testbase'", function(err, rows, fields) { if (err) throw err; connection.query("delete user_tables_structure parent_table_name = 'testbase'", function(err, rows, fields) { if (err) throw err; connection.query("delete user_tables_rules parent_table_name = 'testbase'", function(err, rows, fields) { if (err) throw err; connection.query("delete user_tables_columns parent_table_name = 'testbase'", function(err, rows, fields) { if (err) throw err; res.send('true'); }); }); }); });
thanks help!
i guess using node-mysql. docs says:
support multiple statements disabled security reasons (it allows sql injection attacks if values not escaped).
multiple statement queries
to use feature have enable connection:
var connection = mysql.createconnection({multiplestatements: true});
once enabled, can execute queries multiple statements separating each statement semi-colon ;
. result array each statement.
example
connection.query('select ?; select ?', [1, 2], function(err, results) { if (err) throw err; // `results` array 1 element every statement in query: console.log(results[0]); // [{1: 1}] console.log(results[1]); // [{2: 2}] });
so if have enabled multiplestatements
, first code should work.
Comments
Post a Comment