插入行时

时间:2017-10-30 13:39:27

标签: javascript mysql node.js express

在我的数据库中插入行时出现问题, 它添加了行但在此之后发送此解析错误。 从前端我发送一个JSON POST请求,并在后端发送 我从npm安装了mysql并用它来连接数据库。

问题在于查询中的第一个\如何禁用该内容或任何内容 否则我能做到吗?如果我从查询中删除它们,它会起作用 我已尝试使用INSERT INTO VALUES,它也不起作用。

mysql debug:

  0|index    | { name: 'rette',
  0|index    |   groupMuscles: 'Back',
  0|index    |   datum: '2017-10-29',
  0|index    |   reps1: '2',
  0|index    |   kgs1: '2',
  0|index    |   reps2: '2',
  0|index    |   kgs2: '2',
  0|index    |   reps3: '2',
  0|index    |   kgs3: '2' }
  0|index    | --> ComQueryPacket
  0|index    | ComQueryPacket {
  0|index    |   command: 3,
  0|index    |   sql: 'INSERT INTO `EXERCISE` SET `name` = \'rette\', `groupMuscles` = \'Back\', `datum` = \'2017-10-29\', `reps1` = \'2\', `kgs1` = \'2\', `reps2` = \'2\', `kgs2` = \'2\', `reps3` = \'2\', `kgs3` = \'2\'' }
  0|index    | <-- OkPacket
  0|index    | OkPacket {
  0|index    |   fieldCount: 0,
  0|index    |   affectedRows: 1,
  0|index    |   insertId: 25,
  0|index    |   serverStatus: 2,
  0|index    |   warningCount: 0,
  0|index    |   message: '',
  0|index    |   protocol41: true,
  0|index    |   changedRows: 0 }
  0|index    | {}
  0|index    | --> ComQueryPacket
  0|index    | ComQueryPacket { command: 3, sql: 'INSERT INTO `EXERCISE` SET ' }
  0|index    | <-- ErrorPacket
  0|index    | ErrorPacket {
  0|index    |   fieldCount: 255,
  0|index    |   errno: 1064,
  0|index    |   sqlStateMarker: '#',
  0|index    |   sqlState: '42000',
  0|index    |   message: '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 \'\' at line 1' }
  0|index    | 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 '' at line 1
  0|index    |     at Query.Sequence._packetToError (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
  0|index    |     at Query.ErrorPacket (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
  0|index    |     at Protocol._parsePacket (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/Protocol.js:279:23)
  0|index    |     at Parser.write (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/Parser.js:76:12)
  0|index    |     at Protocol.write (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/Protocol.js:39:16)
  0|index    |     at Socket.<anonymous> (/home/brad/node-js-sample/node_modules/mysql/lib/Connection.js:103:28)
  0|index    |     at emitOne (events.js:96:13)
  0|index    |     at Socket.emit (events.js:188:7)
  0|index    |     at readableAddChunk (_stream_readable.js:176:18)
  0|index    |     at Socket.Readable.push (_stream_readable.js:134:10)
  0|index    |     --------------------
  0|index    |     at Protocol._enqueue (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/Protocol.js:145:48)
  0|index    |     at Connection.query (/home/brad/node-js-sample/node_modules/mysql/lib/Connection.js:208:25)
  0|index    |     at app.post (/home/brad/node-js-sample/index.js:44:6)
  0|index    |     at Layer.handle [as handle_request] (/home/brad/node-js-sample/node_modules/express/lib/router/layer.js:95:5)
  0|index    |     at next (/home/brad/node-js-sample/node_modules/express/lib/router/route.js:137:13)
  0|index    |     at Route.dispatch (/home/brad/node-js-sample/node_modules/express/lib/router/route.js:112:3)
  0|index    |     at Layer.handle [as handle_request] (/home/brad/node-js-sample/node_modules/express/lib/router/layer.js:95:5)
  0|index    |     at /home/brad/node-js-sample/node_modules/express/lib/router/index.js:281:22
  0|index    |     at Function.process_params (/home/brad/node-js-sample/node_modules/express/lib/router/index.js:335:12)
  0|index    |     at next (/home/brad/node-js-sample/node_modules/express/lib/router/index.js:275:10)

index.js

  app.post('/add',(request,response)=>{
    let pushToSrv = request.body;

    //insert new JSON

    // `name` = \'name\' not accepted .)

    let sql = "INSERT INTO `EXERCISE` SET ?";


  db.query(sql,pushToSrv,(err2,resPush) => {
      if(err2)
        throw err2;
        else {

    response.status(200).end('added');
            }
      })//end_insert

  });

1 个答案:

答案 0 :(得分:0)

在执行查询之前,您需要填写pushToSrv

How to test