使用mysql模块为Nodejs存储JSON

时间:2017-07-14 14:19:36

标签: javascript mysql json node.js node-mysql

我需要在列'数据'中存储JSON。这张表:

enter image description here

我试图存储一个json但我收到错误,可能是由于引用...

connection.connect();

var query = 'INSERT INTO Forecasts (data, createdAt, updatedAt, OrchardId) VALUES ?';


connection.query(query, [['{"id": 1}', '2017-07-06 16:08:19','2017-07-06 16:08:19',3]],function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});

connection.end();

输出

  

$ npm start

     
    

revo-weather@1.0.0 start / Users / giovannimarino / Desktop / revo-weather     node index.js

  
     

{" id":1}插入预测(data,createdAt,updatedAt,OrchardId)   价值观(" gio"," 2017-07-06 16:08:13"," 2017-07-06 16:08:13",3)   MacBook-Pro-di-Giovanni:revo-weather giovannimarino $ npm start

     
    

revo-weather@1.0.0 start / Users / giovannimarino / Desktop / revo-weather     node index.js

  
     

/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/Parser.js:79           扔错了; //重新抛出非MySQL错误           ^

     

错误:ER_PARSE_ERROR:您的SQL语法中有错误;检查   手册,对应右边的MySQL服务器版本   语法使用在'' id \":1}',' 2017-07-06 16:08:19', ' 2017年7月6日   16:08:19',3'在第1行       在Query.Sequence._packetToError(/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)       在Query.ErrorPacket(/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)       在Protocol._parsePacket(/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/Protocol.js:280:23)       在Parser.write(/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/Parser.js:75:12)       在Protocol.write(/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/Protocol.js:39:16)       在Socket。 (/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/Connection.js:103:28)       在emitOne(events.js:115:13)       在Socket.emit(events.js:210:7)       在addChunk(_stream_readable.js:252:12)       在readableAddChunk(_stream_readable.js:239:11)       --------------------       在Protocol._enqueue(/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/Protocol.js:141:48)       在Connection.query(/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/Connection.js:208:25)       在对象。 (/Users/giovannimarino/Desktop/revo-weather/index.js:19:12)       在Module._compile(module.js:569:30)       at Object.Module._extensions..js(module.js:580:10)       在Module.load(module.js:503:32)       在tryModuleLoad(module.js:466:12)       在Function.Module._load(module.js:458:3)       在Function.Module.runMain(module.js:605:10)       在启动时(bootstrap_node.js:158:16)npm ERR!代码ELIFECYCLE npm ERR!错误1 npm ERR! revo-weather@1.0.0 start:node index.js npm   呃!退出状态1 npm ERR!错误的ERR!在revo-weather@1.0.0失败   启动脚本。错误的ERR!这可能不是npm的问题。那里   可能是上面的额外日志输出。

     

npm ERR!可以在以下位置找到此运行的完整日志:npm ERR!
  /Users/giovannimarino/.npm/_logs/2017-07-14T14_16_03_166Z-debug.log

2 个答案:

答案 0 :(得分:1)

你如何JSON.strigify你的json对象,然后坚持下去。在撤回数据时,您可以再次解析它。

connection.connect();

var query = 'INSERT INTO Forecasts (data, createdAt, updatedAt, OrchardId) VALUES ?';


connection.query(query, [[JSON.stringify('{"id": 1}'), '2017-07-06 16:08:19','2017-07-06 16:08:19',3]],function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});

connection.end();

答案 1 :(得分:0)

您可以简单地使用:

JSON.stringify(data)