mysql - json 到 mysql 使用 node.js

标签 mysql json node.js insert

我试图将在 node.js 中创建的 json 插入到 mysql 中, 但是语法有错误,我无法纠正错误, 任何帮助将不胜感激

我的代码

flowController.on('2', function (_selfid,_participantId,_groupid,_allMemberContent) 
        {
    var allMemberDetailSQL= "SELECT spFunAllMemberNotificationDetails("+ _selfid + "," + _participantId +") as groupparticipants";
    console.log("allMemberDetailSQL"+allMemberDetailSQL);

client.query(allMemberDetailSQL,function(detailERROR,detailResult)
        {
            if (detailERROR)
                console.log("detailERROR "+ detailERROR);
            else 
            {

                var detailstr='';
                detailstr = JSON.stringify(detailResult);
                console.log('detailResult :'+ detailstr);
                console.log("detailResult "+detailResult[0].groupparticipants);
                var otherArray = [detailResult[0].groupparticipants];


                var _allMemberDetail = JSON.stringify({
                    selfid: _selfid,
                    groupid: _groupid, 
                    anArray: otherArray
                  });

                console.log("_allMemberDetail " +_allMemberDetail);

                var allMemberDetail = "'"+_allMemberDetail+"'";
                console.log("allMemberDetail "+allMemberDetail);
                client.query("INSERT INTO cmNotification (notificationSenderId, notificationReceiverId)"+"VALUES('"+_selfid+"','"+ _allMemberDetail+ "');", function(err, rows)
                         {
                            console.log("error insert "+err);
                            console.log("rows insert"+rows);
                            //connection.release();

                          });
            }

        });

});

控制台输出

allMemberDetailSQLSELECT spFunAllMemberNotificationDetails(20,16) as groupparticipants
detailResult :[{"groupparticipants":"userid:'15',firstname:'pitu15',lastname:'',isfriend:'1',profilepicurl:''"}]
detailResult userid:'15',firstname:'pitu15',lastname:'',isfriend:'1',profilepicurl:''
_allMemberDetail {"selfid":"20","groupid":"15","anArray":["userid:'15',firstname:'pitu15',lastname:'',isfriend:'1',profilepicurl:''"]}
allMemberDetail '{"selfid":"20","groupid":"15","anArray":["userid:'15',firstname:'pitu15',lastname:'',isfriend:'1',profilepicurl:''"]}'
detailResult :[{"groupparticipants":"userid:'16',firstname:'pitu16',lastname:'',isfriend:'0',profilepicurl:''"}]
detailResult userid:'16',firstname:'pitu16',lastname:'',isfriend:'0',profilepicurl:''
_allMemberDetail {"selfid":"20","groupid":"15","anArray":["userid:'16',firstname:'pitu16',lastname:'',isfriend:'0',profilepicurl:''"]}
allMemberDetail '{"selfid":"20","groupid":"15","anArray":["userid:'16',firstname:'pitu16',lastname:'',isfriend:'0',profilepicurl:''"]}'
error insert 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 '15',firstname:'pitu15',lastname:'',isfriend:'1',profilepicurl:''"]}')' at line 1
rows insertundefined
error insert 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 '16',firstname:'pitu16',lastname:'',isfriend:'0',profilepicurl:''"]}')' at line 1
rows insertundefined

最佳答案

使用内置 parameters escaping防止sql注入(inject)攻击。 “插入……设置?”也让生活更轻松:

client.query("INSERT INTO cmNotification SET ?",  {notificationSenderId: _selfid, notificationReceiverId: _allMemberDetail}, function(err, rows) {
  // ...
});

关于mysql - json 到 mysql 使用 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22983777/

相关文章:

mysql - 最后一行的总计查询

php - Backbone.js:奇怪的 .save() 错误回调

node.js - Webpack/babel 意外 token ,预期为 ";"

python - 从python for Excel中的json数组中提取数据

class - NodeJS 模块与类

javascript - 当我想使用 adonis js 在 Windows 操作系统中上传图像时,我发现这个问题,我该如何解决这个问题

mysql - 查询以返回所有行的所有重复项

php - 如果其值在数据库中,请选中复选框

mysql - 在 mysql 的内容中搜索和替换图像尺寸?

javascript - 回调执行得太早