Node js mysql多语句查询占位符



我的项目快结束了。我只需要写最后的查询数据库。我想使用占位符来做到这一点,但我不知道如何在多语句查询中做到这一点:(.

这是我的查询:

var query3 = 'UPDATE accounts SET balance = ((SELECT balance FROM accounts WHERE uid = ?) - ?) WHERE uid = ?;';
query3 =+ 'UPDATE accounts SET balance = ((SELECT balance FROM accounts WHERE uid = ?) + ?) WHERE uid = ?;';
query3 =+ 'INSERT INTO transactions SET ?';

我试过这样做:

db.multi.query(query3, [result1[0].id, amount, result1[0].id], [destination, amount, destination], {name: name, source_id: result1[0].id, destination_id: destination, amount: amount, type: 1}, (err3, result3) =>
{
if (err3) throw err3;
res.redirect('/account/transfer_success');
})

:

db.multi.query(query3, [result1[0].id, amount, result1[0].id, destination, amount, destination, {name: name, source_id: result1[0].id, destination_id: destination, amount: amount, type: 1}], (err3, result3) =>
{
if (err3) throw err3;
res.redirect('/account/transfer_success');
})

控制台错误:TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是字符串类型,或者是Buffer、ArrayBuffer、Array或类数组对象的实例。接收类型号(NaN)

请告诉我如何正确地做这件事。注:我使用的是mysql2包。事务表:id name source_id destination_id amount type帐户表:id名称email密码余额状态

在MySQL中,你不能执行包含多个SQL语句的准备查询。

https://dev.mysql.com/doc/refman/en/prepare.html:

文本必须代表一个语句,而不是多个语句。

https://dev.mysql.com/doc/c-api/en/c-api-multiple-queries.html

多语句和结果功能只能用于mysql_real_query()mysql_query()。它们不能与预处理语句接口一起使用。预处理语句处理程序被定义为只处理包含单个语句的字符串。

即使不使用参数化查询,使用多查询也没有实际的好处。只需在单独的调用中一次执行一个查询。然后,您可以使用带参数的准备查询。

最新更新