node-mysql一次查询多条语句

4ktjp1zp  于 2023-08-02  发布在  Mysql
关注(0)|答案(4)|浏览(100)

我正在使用nodejs 10.26 + express 3.5 + node-mysql 2.1.1 + MySQL-Server Version: 5.6.16
我得到了4个DELETE,只需要1个数据库请求,所以我用一个“;“但它总是失败。

var sql_string = "DELETE FROM user_tables WHERE name = 'Testbase';";
sql_string += "DELETE FROM user_tables_structure WHERE parent_table_name = 'Testbase';";
sql_string += "DELETE FROM user_tables_rules WHERE parent_table_name = 'Testbase';";
sql_string += "DELETE FROM user_tables_columns WHERE parent_table_name = 'Testbase';";

connection.query(sql_string, function(err, rows, fields) {
   if (err) throw err;
   res.send('true');
});

字符串
它抛出这个错误:

Error: ER_PARSE_ERROR: 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 'DELETE FROM user_tables_structure WHERE parent_table_name = 'Testbase';DELETE FR' at line 1


但如果我粘贴这个SQL在PhpMyAdmin它总是成功的.
如果我用一个查询来写,它也成功了。

connection.query("DELETE FROM user_tables WHERE name = 'Testbase'", function(err, rows, fields) {
        if (err) throw err;

        connection.query("DELETE FROM user_tables_structure WHERE parent_table_name = 'Testbase'", function(err, rows, fields) {
            if (err) throw err;

            connection.query("DELETE FROM user_tables_rules WHERE parent_table_name = 'Testbase'", function(err, rows, fields) {
                if (err) throw err;

                connection.query("DELETE FROM user_tables_columns WHERE parent_table_name = 'Testbase'", function(err, rows, fields) {
                    if (err) throw err;

                    res.send('true');
                });
            });
        });
    });


谢谢帮忙!

mv1qrgav

mv1qrgav1#

我猜你正在使用node-mysql。(* 但也适用于node-mysql2*)
医生说:
出于安全原因,禁用了对多语句的支持(如果值未正确转义,则允许SQL注入攻击)。

多语句查询

要使用此功能,您必须为连接启用此功能:

var connection = mysql.createConnection({multipleStatements: true});

字符串
启用后,您可以通过使用分号;分隔每条语句来执行多条语句的查询。结果将是每个语句的数组。

示例

connection.query('SELECT ?; SELECT ?', [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}]
});


因此,如果您启用了multipleStatements,那么您的第一个代码应该可以工作。

gkl3eglg

gkl3eglg2#

使用“multiplestatements:真正的“像下面所示的那样为我工作

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: '',
    multipleStatements: true
});
connection.connect();
 
var sql = "CREATE TABLE test(id INT DEFAULT 1, name VARCHAR(50));ALTER TABLE test ADD age VARCHAR(10);";
 
connection.query(sql, function(error, results, fields) {
    if (error) {
        throw error;
    }
});

字符串

wpx232ag

wpx232ag3#

从DB(SQL)中取数据,以下函数将准确工作

router.get('/',function messageFunction(req,res){//res.send('Hi Dear Rasikh,Welcome to Test Page.')//=>单向dbConn.query('SELECT COUNT(name)as counted,name,last_name,phone,email from students',function(err,rows,fields){ // another Way if(err)throw err

dbConn.query('SELECT name, author from books',
  function (err, rowsBook, fields) { // another Way
      if (err) throw err
    // console.log('The counted is: ', rows[0].counted);    //=> Display in console
    // res.send('Hi Dear Rasikh, Welcome to Test Page.'+ rows[0].counted)  //=> Display in blank page
    
    res.render('main/index',{data:rows, myData:rowsBook});
  })

字符串
}); });

sr4lhrrt

sr4lhrrt4#

我在Next.js中工作了...

export default async function handler(req, res) {
  try {
    // Build your multiple MySQL queries here
    const querySql = "DELETE FROM favorites WHERE user = ? AND listingID= ?";
    const querySql2 = "UPDATE properties SET saves = ? WHERE listingid = ?";

    // Pass any params here
    const valuesParams = [user, listingID];
    const valuesParams2 = [upDateSaves, listingID];

    //Execute your multiple MySQL queries here
    const data = await query({query: querySql, values: valuesParams });
    const data2 = await query({query: querySql2, values: valuesParams2 });

    //Combine the results
    const combinedResults = [data, data2];

    res.status(200).json({ 
    text: combinedResults,
   });

  } catch (error) {
    res.status(500).json({ error: 'Error fetching data' });
  }
}

字符串

相关问题