gpt4 book ai didi

javascript - 插入行时解析错误

转载 作者:行者123 更新时间:2023-11-29 18:16:03 26 4
gpt4 key购买 nike

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

问题在于查询中的第一个\我如何禁用它或任何东西还有什么我可以做的吗?如果我从查询中删除它们,它就会起作用。
我尝试过 INSERT INTO VALUES 但它也不起作用。

mysql调试:

  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

});

最佳答案

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

How to test

关于javascript - 插入行时解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47016715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com