我有代码。
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
字段中都有关系。有人知道为什么数据没有插入地址表吗?
您可能需要分别执行每个查询。所以试试这个:
connection.query(`${addressSQL}`, (err, result) => {
connection.query(`${clientSQL}`, (err, result_) => {
// If no errors, two statements executed
});
});
请阅读节点mysql文档
截面连接选项
multipleStatements:允许每个查询有多个mysql语句。小心这样,它可能会增加SQL注入攻击的范围。(默认值:false)