使用 Node.JS 将 JSON 对象数据插入 MySQL [英] Insert JSON object data to MySQL using Node.JS

查看:71
本文介绍了使用 Node.JS 将 JSON 对象数据插入 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Node.js 服务器的 MySQL 中插入一个 JSON 对象,这是代码

I want insert a JSON object into MySQL in Node.js server, this is the code

let id = 1
let date = new Date().toJSON().slice(0,10).replace(/-/g,'/');
let sql ='INSERT INTO case_record (case_details,gen_date,case_id) VALUES('+caseDetails+','+date+','+id+')'
console.log(sql)
con.query(sql,function(err, result, fields){
    if(err) throw err;
    res = result;
    console.log(res)
});

这是caseDetails数据

This is the caseDetails data

let caseDetails = {
    caseData,
    patData, 
    notifData,
    primecData, 
    refData}

caseDetails 中的每个对象也是 JSON 对象.当我执行时,错误返回是

Each of the object in the caseDetails is JSON object also. When I excute, the error return is

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 '[object Object],2019/04/22,1)' at line 1

如何解决这个问题?

推荐答案

你的SQL语法错误导致解析错误.

Your SQL syntax is wrong to cause parsing error.

你为什么不遵循这个更正?

Why don't u follow this correction?

...
let sql ='INSERT INTO case_record(case_details,gen_date,case_id) VALUES(?,?,?)';
con.query(sql, [caseDetails,date,id] ,function(err, result, fields) {
  ...
});

希望得到帮助.

这篇关于使用 Node.JS 将 JSON 对象数据插入 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆