2017-10-30 2 views
0

データベースに行を挿入するときに問題があります。 行を追加しますが、その後にこの解析エラーが送信されます。 フロントエンドから私は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 

    }); 
+0

「'EXERCISE'のSET。INSERT INTO」完全なSQL文 – JochenJung

+0

ではありませんこれは、テーブルの行を追加しますが、その後は、このエラーをログに記録します。 – zivce

+0

クエリを実行する前に、 'pushToSrv'を満たしていることを確認する必要があります。 'if(pushToSrv instanceof Array && pushToSrv.length)' ... –

答えて

0

あなたがpushToSrvを埋めるチェック必要なクエリを実行する前に。

How to test

+0

なぜそれが必要ですか? – zivce

+0

あなたの例では、 'pushToSrv'が空のときだけエラーが発生します。 –

+0

okありがとう! – zivce

関連する問題