MySQL 在 NodeJS 中不适用于 INSERT INTO 中的多个语句
MySQL in NodeJS is not working for multiple statements in INSERT INTO
我有代码。
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);
当我 运行 手动查询时,数据被插入到 tables 中,但是当我在节点中使用 mysql 模块时,仅插入客户端数据,而没有插入任何内容 addresses
table。只有未插入的数据没有来自模块的错误消息。两个 table 都在 postalCode
字段中有关系。任何人都知道为什么数据没有插入地址 table?
您可能必须分别执行每个查询。所以试试这个:
connection.query(`${addressSQL}`, (err, result) => {
connection.query(`${clientSQL}`, (err, result_) => {
// If no errors, two statements executed
});
});
请阅读节点-mysql 文档
section connection option
multipleStatements: Allow multiple mysql statements per query. Be careful
with this, it could increase the scope of SQL injection attacks.
(Default: false)
我有代码。
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);
当我 运行 手动查询时,数据被插入到 tables 中,但是当我在节点中使用 mysql 模块时,仅插入客户端数据,而没有插入任何内容 addresses
table。只有未插入的数据没有来自模块的错误消息。两个 table 都在 postalCode
字段中有关系。任何人都知道为什么数据没有插入地址 table?
您可能必须分别执行每个查询。所以试试这个:
connection.query(`${addressSQL}`, (err, result) => {
connection.query(`${clientSQL}`, (err, result_) => {
// If no errors, two statements executed
});
});
请阅读节点-mysql 文档
section connection option
multipleStatements: Allow multiple mysql statements per query. Be careful with this, it could increase the scope of SQL injection attacks. (Default: false)