简体   繁体   English

当我想用 nodejs 插入 mysql 时,为什么会出现解析错误

[英]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.

相关问题 为什么在尝试解析这个 json 对象时会出错? - Why do I get error when trying to parse this json object? 为什么在尝试使用 NodeJS 和 Tedious 将数据插入到 SQL Server 时会出现此错误? - Why do I get this error while trying to insert data to SQL Server with NodeJS and Tedious? 为什么要发布JSON文件HTTP REQUEST时出现错误400 - Why do I get a Error 400 when I want to post a JSON file HTTP REQUEST 当我尝试在JavaScript中解析JSON时为什么会出错 - Why I get error when I try to parse JSON in the JavaScript 为什么在运行 nodejs express 服务器时出现错误:找不到模块 'html'? - Why do I get Error: Cannot find module 'html' when running nodejs express server? 我想用nodejs解析这个json数据,但是得到未定义的错误 - i want to parse this json data with nodejs but gets error undefined 当我提出请求时,为什么我会得到未处理的承诺错误 - why do i get unhandled promise error when i make get request 为什么在nodejs中`或`大量时我得到负值? - Why do I get negative value when `or` a large number in nodejs? 为什么当我拨打 http 时,会收到一条错误消息说我的 url 不是字符串? - Why, when I make an http call, do I get an error saying my url is not a string? 如何解析xml以将其插入到mysql数据库中? - How do I parse xml to insert it into a mysql database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM