【问题标题】:Store JSON using mysql module for Nodejs使用 mysql 模块为 Nodejs 存储 JSON
【发布时间】:2017-12-19 16:32:53
【问题描述】:

我需要在此表的“数据”列中存储一个 JSON:

我尝试存储一个 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 开始

revo-weather@1.0.0 开始 /Users/giovannimarino/Desktop/revo-weather 节点索引.js

{"id":1} INSERT INTO Forecasts (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 开始 /Users/giovannimarino/Desktop/revo-weather 节点索引.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-07-06' 附近使用的语法 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) 在套接字。 (/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) 在 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 错误! errno 1 npm 错误! revo-weather@1.0.0 开始:node index.js npm 呃!退出状态 1 npm ERR! npm 错误!在 revo-weather@1.0.0 失败 启动脚本。 npm 错误!这可能不是 npm 的问题。那里 可能是上面的额外日志输出。

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

【问题讨论】:

  • 只是一个意见,但如果你想以 JSON 格式保存数据,请查看 MongoDB

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


【解决方案1】:

你可以简单地使用:

JSON.stringify(data)

【讨论】:

    【解决方案2】:

    你怎么样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();
    

    【讨论】:

    • 我收到此错误: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 ''\"{\\\"id\\\": 1}\"', '2017-07-06 16:08:19', '2017-07-06 16:08:19', 3' at line 1
    猜你喜欢
    • 2014-07-29
    • 2013-06-18
    • 1970-01-01
    • 2014-08-04
    • 2017-07-05
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    相关资源
    最近更新 更多