[英]Why do i get parse error when i want to make an mysql insert with nodejs
I have an sql insert and I recive the following error:我有一个 sql 插入,我收到以下错误:
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''CR0001'' at line 1",
sqlState: '42000',
index: 0,
sql: "INSERT INTO coordonate (id_scooter,lat,longi,alt,ip,port,speed,nr_satelites,battery_lvl) VALUES 'CR0001' "
my code is:我的代码是:
var insert = "INSERT INTO coordonate (id_scooter,lat,longi,alt,ip,port,speed,nr_satelites,battery_lvl) VALUES ? ";
con.query(insert, [array[0],array[3],array[4],array[5],rinfo.address,rinfo.port,array[6],array[2],array[1]], function (err, result) {
if (err) throw err;
You are only passing in a single array.您只传入一个数组。 When you insert, it needs to be an array of arrays wrapped in an array, like this:
当你插入时,它需要是一个包含在一个数组中的 arrays 的数组,像这样:
var insert = "INSERT INTO coordonate (id_scooter,lat,longi,alt,ip,port,speed,nr_satelites,battery_lvl) VALUES ? ";
con.query(insert, [ [ [array[0],array[3],array[4],array[5],rinfo.address,rinfo.port,array[6],array[2],array[1]] ] ], function (err, result) {
if (err) throw err;
});
Notice the two extra [
before and two extra ]
after your data in the query.请注意查询中数据之后的两个额外
[
before 和两个 extra ]
。
[ [ [array[0],array[3],array[4],array[5],rinfo.address,rinfo.port,array[6],array[2],array[1]] ] ]
As an alternative, if you want to only insert a single row, you could do something like this, and pass in a JSON object:作为替代方案,如果您只想插入一行,您可以执行以下操作,并传入 JSON object:
var insert = "INSERT INTO coordonate SET ? ";
con.query(insert, { id_scooter: array[0], lat: array[3], longi: array[4], alt: array[5], ip: rinfo.address, port: rinfo.port, speed: array[6], nr_satelites: array[2], battery_lvl: array[1] }, function (err, result) {
if (err) throw err;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.