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

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

Patricia Arquette
Release: 2024-12-09 22:30:25
Original
634 people have browsed it

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

Using Multiple Statements in One Query with Node-MySQL

Node.js developers often encounter the need to execute multiple database statements in a single query. While convenient, this practice has raised security concerns, leading node-mysql to disable support for multiple statements by default.

This article addresses this issue by exploring how to enable multiple statement queries in node-mysql.

Enabling Multiple Statements

To execute multiple statements, you must first enable the feature in your connection:

const connection = mysql.createConnection({multipleStatements: true});
Copy after login

Syntax and Execution

Once enabled, you can execute queries with multiple statements by separating each statement with a semicolon (;). The result returned is an array containing one element for each statement executed.

Example

connection.query('SELECT ?; SELECT ?', [1, 2], (err, results) => {
  if (err) throw err;
  
  console.log(results[0]); // [{1: 1}]
  console.log(results[1]); // [{2: 2}]
});
Copy after login

In your specific case, with multiple DELETE statements, ensure that you have enabled multiple statements and that the syntax is correct, as shown in your sample code.

The above is the detailed content of How Can I Execute Multiple SQL Statements in a Single Query with Node-MySQL?. 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