NodeJS中的MySQL不适用于INSERT INTO中的多个语句

时间:2016-05-02 01:08:35

标签: javascript mysql sql node.js node-mysql

我遵守了代码。

let addressQuery = 'INSERT INTO addresses(postalCode, street, city, state) VALUES(?, ?, ?, ?);';
let addressValue = [client.postalCode.replace(/\-/gi, ''), client.address, client.city, client.state];

let clientQuery = `INSERT INTO client(name, email, password, postalCode, addressNumber, cellPhone) VALUES(?, ?, ?, ?, ?, ?);`;

let clientValues = [client.name, client.email, client.password, client.postalCode.replace(/\-/gi, ''), client.number, client.cellPhone ];

let addressSQL = mysql.format(addressQuery, addressValue);
let clientSQL = mysql.format(clientQuery, clientValues);

connection.query(`${addressSQL} ${clientSQL}`, (err, result) => {});

addressSQL

中生成我的查询
INSERT INTO addresses(postalCode, street, city, state) VALUES('04545041', 'Rua Santa Justina', 'São Paulo', 'SP');`

clientSQL

INSERT INTO client(name, email, password, postalCode, addressNumber, cellPhone) VALUES('Keila', 'keila@dressandgo.com.br', 'Senha123Forte', '04545041', 352, 1130454006);

当我手动运行查询时,数据被插入到表中,但是当我在节点中使用mysql模块时,只插入了客户端数据,并且在addresses表中没有插入任何内容。没有插入数据的模块没有错误消息。两个表在postalCode字段中都有关系。任何人都知道为什么没有在地址表中插入数据?

2 个答案:

答案 0 :(得分:1)

您可能必须单独执行每个查询。所以试试这个:

connection.query(`${addressSQL}`, (err, result) => {
   connection.query(`${clientSQL}`, (err, result_) => {
     // If no errors, two statements executed
   });
});

答案 1 :(得分:0)

请阅读node-mysql文档

  

部分连接选项

     

multipleStatements:每个查询允许多个mysql语句。小心   有了它,它可能会增加SQL注入攻击的范围。   (默认值:false)