使用 node.js 在 mysql 中插入多条记录失败
Insert multiple records in mysql using node.js fails
我试图在 MySQL Table 中插入多条记录,但无论我做什么,它都只插入一行。
这里是 node.js 代码:
let orderID = 15; //It is a foreign key. Noted it from the table.
itemParams = [
[ orderID, 'd', 34.6, '12', '123.jpeg' ],
[ orderID, 'd', 30.6, '2', '456.jpeg' ]
];
let addOrderItems = await pool.query(`INSERT INTO orderitems (oid, description, weight, quantity, image_url) VALUES (?)`, itemParams);
当我控制台记录 addOrderItems 时,affectedRows 计数为 1,因此我还检查了 Workbench 中的 table 以确保只添加了一行。 (oid 是外键)。 Table 确实有一个自动递增的主键。
我在网上找到的查询没有在问号周围加上括号?但是当我删除括号时,出现以下错误:
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 '19, 'd', 34.6, '12', '123.jpeg'' at line 1
at Query.Sequence._packetToError (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
at Protocol._parsePacket (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (Z:\Jura_SaaS\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (Z:\Jura_SaaS\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:309:12)
--------------------
at Pool.query (Z:\Jura_SaaS\node_modules\mysql\lib\Pool.js:199:23)
at internal/util.js:297:30
at new Promise (<anonymous>)
at Pool.query (internal/util.js:296:12)
at Z:\Jura_SaaS\routes\order.js:190:44
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: "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 '19, 'd', 34.6, '12', '123.jpeg'' at line 1",
sqlState: '42000',
index: 0,
sql: "INSERT INTO orderitems (oid, description, weight, quantity, image_url) VALUES 19, 'd', 34.6, '12', '123.jpeg';"
}
注意:我已经承诺 pool.query 这就是您看不到它的回调格式的原因。
嗯,我一直在用类似的方法。除了我没有一次插入所有行而是通过在循环的每次迭代中执行查询来执行每一行的插入这一事实。
注意:我使用的是 MVC 服务器架构
这是代码。
- 控制器
module.export.multiInsert = async (req) => {
const transferData = req.body.rows;
let conn = await con.getConnection();
for await (data of transferData) {
try {
await conn.beginTransaction();
// code for insertion
const data = await something.addData(conn, a, b, c);
} catch (error) {
conn.rollback();
response.status = false;
response.result = 'Error occured!'
console.log(error)
} finally {
conn.release();
}
}
- 型号
Class something{
static async addData(conn, a, b, c) {
let params = [a,b,c];
const [resData] = await conn.execute('INSERT INTO `table` (`a`,`b`,`c`)
VALUES(?,?,?);', params);
return { "status": resData };
}
}
- 路线
router.post('/insert-a-lot-of-data', controller.multiInsert)
我遇到了同样的问题。这是一份 article 详细说明您的案例。对于您的情况,我会执行以下操作:
let orderID = 15; //It is a foreign key. Noted it from the table.
itemParams = [
[ orderID, 'd', 34.6, '12', '123.jpeg' ],
[ orderID, 'd', 30.6, '2', '456.jpeg' ]
];
let addOrderItems = await pool.query(`INSERT INTO orderitems (oid, description, weight, quantity, image_url) VALUES ?`, [itemParams]);
我试图在 MySQL Table 中插入多条记录,但无论我做什么,它都只插入一行。
这里是 node.js 代码:
let orderID = 15; //It is a foreign key. Noted it from the table.
itemParams = [
[ orderID, 'd', 34.6, '12', '123.jpeg' ],
[ orderID, 'd', 30.6, '2', '456.jpeg' ]
];
let addOrderItems = await pool.query(`INSERT INTO orderitems (oid, description, weight, quantity, image_url) VALUES (?)`, itemParams);
当我控制台记录 addOrderItems 时,affectedRows 计数为 1,因此我还检查了 Workbench 中的 table 以确保只添加了一行。 (oid 是外键)。 Table 确实有一个自动递增的主键。
我在网上找到的查询没有在问号周围加上括号?但是当我删除括号时,出现以下错误:
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 '19, 'd', 34.6, '12', '123.jpeg'' at line 1
at Query.Sequence._packetToError (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
at Protocol._parsePacket (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (Z:\Jura_SaaS\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (Z:\Jura_SaaS\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (Z:\Jura_SaaS\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:309:12)
--------------------
at Pool.query (Z:\Jura_SaaS\node_modules\mysql\lib\Pool.js:199:23)
at internal/util.js:297:30
at new Promise (<anonymous>)
at Pool.query (internal/util.js:296:12)
at Z:\Jura_SaaS\routes\order.js:190:44
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: "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 '19, 'd', 34.6, '12', '123.jpeg'' at line 1",
sqlState: '42000',
index: 0,
sql: "INSERT INTO orderitems (oid, description, weight, quantity, image_url) VALUES 19, 'd', 34.6, '12', '123.jpeg';"
}
注意:我已经承诺 pool.query 这就是您看不到它的回调格式的原因。
嗯,我一直在用类似的方法。除了我没有一次插入所有行而是通过在循环的每次迭代中执行查询来执行每一行的插入这一事实。
注意:我使用的是 MVC 服务器架构
这是代码。
- 控制器
module.export.multiInsert = async (req) => {
const transferData = req.body.rows;
let conn = await con.getConnection();
for await (data of transferData) {
try {
await conn.beginTransaction();
// code for insertion
const data = await something.addData(conn, a, b, c);
} catch (error) {
conn.rollback();
response.status = false;
response.result = 'Error occured!'
console.log(error)
} finally {
conn.release();
}
}
- 型号
Class something{
static async addData(conn, a, b, c) {
let params = [a,b,c];
const [resData] = await conn.execute('INSERT INTO `table` (`a`,`b`,`c`)
VALUES(?,?,?);', params);
return { "status": resData };
}
}
- 路线
router.post('/insert-a-lot-of-data', controller.multiInsert)
我遇到了同样的问题。这是一份 article 详细说明您的案例。对于您的情况,我会执行以下操作:
let orderID = 15; //It is a foreign key. Noted it from the table.
itemParams = [
[ orderID, 'd', 34.6, '12', '123.jpeg' ],
[ orderID, 'd', 30.6, '2', '456.jpeg' ]
];
let addOrderItems = await pool.query(`INSERT INTO orderitems (oid, description, weight, quantity, image_url) VALUES ?`, [itemParams]);