2016-09-21 32 views
0

我试过寻找这个,但大多数结果需要我来批量插入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

我该怎么做?

+0

你不叫'2 rows','多rows'? – Shaharyar

+0

不,我不这么认为?我只想将new_Domicilio插入一行 – Alan

+0

您的数组中有2个对象,表示2行。你怎么能把它保存到一行?你的表结构是什么? – Shaharyar

回答

0

如果你想将其保存在一个row然后不裹在[]

试试这个

connection.query("INSERT INTO `domicilio` SET ?", new_Domicilio, function(err, results) { 
    if(err) { 
    console.log(err); 
    return; 
    } 
+0

这并没有解决这个问题,同样的错误 – Alan

+0

它应该工作,因为它为我工作,你可以包裹'domicilio'并重试? – abdulbarik

+0

同样的错误我不知道什么是错的,我的日志说'Orden {“id”:3,“cantidad”:5},{“id”:1,“cantidad”:2}'甚至包装domicilio并删除[] – Alan

相关问题