Home >Database >Mysql Tutorial >How Can I Execute Multiple SQL Statements in a Single Node-MySQL Query?
Executing Multiple SQL Statements in Single Query Using node-mysql
You mentioned encountering an error when executing multiple DELETE statements in a single query using node-mysql. To address this issue, you should enable support for multiple statements in your connection configuration.
Enabling Multiple Statement Queries
The node-mysql documentation explains that support for multiple statements is initially disabled for security reasons, preventing SQL injection attacks. To enable it, add the following line to your connection configuration:
var connection = mysql.createConnection({multipleStatements: true});
Executing Multiple Statements
Once multiple statements are enabled, you can execute a query with multiple statements by separating each statement with a semicolon (;). The result will be an array containing an array for each statement.
Example
Consider the following code:
connection.query('DELETE FROM user_tables WHERE name = 'Testbase';DELETE FROM user_tables_structure WHERE parent_table_name = 'Testbase';DELETE FROM user_tables_rules WHERE parent_table_name = 'Testbase';DELETE FROM user_tables_columns WHERE parent_table_name = 'Testbase';', [1, 2], function(err, results) { if (err) throw err; // `results` is an array with one element for every statement in the query: console.log(results[0]); // [{1: 1}] console.log(results[1]); // [{2: 2}] });
By enabling multiple statements and properly separating the statements with semicolons, you should be able to execute all your DELETE operations in a single query.
The above is the detailed content of How Can I Execute Multiple SQL Statements in a Single Node-MySQL Query?. For more information, please follow other related articles on the PHP Chinese website!