我试过寻找这个,但大多数结果需要我来批量插入Node.js,我不需要atm。根据我的理解,bulk instert意味着插入多行,这是我不需要的。插入数组作为参数,以在node.js中查询mySQL
我试图使用对象数组作为查询参数:
function saveDomicilio(id_establecimiento, token, order, total){
var order = [{"id":1,"quantity":2},{"id":"3","quantity":"4"}];
console.log("Order" + order); // logs correctly my order
var new_Domicilio = {
id_establecimiento: id_establecimiento,
id_usuario: token,
orden: order,
total : total
};
pool.getConnection(function(err, connection) {
if(err) {
console.log(err);
return;
}
// make the query
connection.query("INSERT INTO domicilio SET ?", [new_Domicilio], function(err, results) {
if(err) {
console.log(err);
return;
}
console.log("New domicilio created");
return;
}); // query
}); // pool
};
似乎与使用数组一个问题,我得到的错误:
{ [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 ''{\"id\":1,\"cantidad\":2}',
total
= 300
我该怎么做?
你不叫'2 rows','多rows'? – Shaharyar
不,我不这么认为?我只想将new_Domicilio插入一行 – Alan
您的数组中有2个对象,表示2行。你怎么能把它保存到一行?你的表结构是什么? – Shaharyar