Home > Database > Mysql Tutorial > How Can I Execute Multiple SQL Statements in a Single Node-MySQL Query?

How Can I Execute Multiple SQL Statements in a Single Node-MySQL Query?

Susan Sarandon
Release: 2024-12-20 09:32:11
Original
636 people have browsed it

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});
Copy after login

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}]
});
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template