1

我是 Node.js 的初学者。请帮助解决这个问题。

不提供WHERE子句更新工作正常。下面是没有“WHERE”子句的脚本:

var post  = {name: 'Amit', mobile:'123456'};
var query = connection.query('UPDATE contacts SET ? ', post , function(err, result) {});
console.log(query.sql);

输出:

在此处输入图像描述

现在我添加了“WHERE”子句..出现错误:

var post  = {name: 'Amit', mobile:'123456'};
var condition = {id:4};
var query = connection.query('UPDATE contacts SET ? ',post,' WHERE '+ condition , function(err, result) {});
console.log(query.sql);

输出:

在此处输入图像描述

4

4 回答 4

3

根据 api,您应该像这样编写查询:

connection.query('UPDATE contacts SET Name = ?,Mobile=? WHERE yourCondition = ?', [post.name,post.mobile,condition], function(err, result) {})
于 2013-05-08T08:52:40.503 回答
1

试试那个代码:

var post  = {name: 'Amit', mobile:'123456'};
var condition = {id:4};
var query = connection.query('UPDATE contacts SET ? WHERE ?', [post, condition] , function(err, result) {});
console.log(query.sql);
于 2015-11-16T12:25:02.740 回答
0

试试这个,它对我有用

connection.query('UPDATE nodeDB.USER SET USER_PASSWORD :Pass WHERE USER_NAME :Name',
  {Name: 'max',Pass: '123'}, function(err, rows) {
}); 
于 2014-02-11T05:50:01.060 回答
0

检查我的代码也适合你。

router.all('/setLoginDetails', function (req, res) {
    if(req.method == 'POST')
    {
        var Req = req.body;
    }
    else
    {
        var Req = req.query;
    }
    if(Req.lang == undefined || Req.lang == 'en')
    {
        const message = require('../lang/messages_en.json');
    }

    if(Req.id == undefined || Req.id == '')
    {
        return res.json({"success" : false, "Message" : message.REQUIRED , "data" : {} });
    }

    qb.select('id')
        .where({id: Req.id})
        .get('table', (err,rows) => {
            if (err || rows.length == 0)
            {

                return res.json({"success" : false, "Message" : "No data found", "data" : Response});
            }
            else
            {
                _.each(rows, function(record) {
                    var token = tokenGenerate();
                    qb.update('token', {token: token}, {driver_id:record.id}, (err, result) => {
                        if (err) return console.error(err);
                    });

                    qb.select('*').where({driver_id: record.id}).get(model.DriverLogin, (err,rows) => {
                        var lat = Req.lat;
                        var lng = Req.lng;
                        if(Req.lat == '')
                        {
                            lat = 0.0;
                        }
                        if(Req.lng == '')
                        {
                            lng = 0.0;
                        }
                        var updateData = {lat : lat ,lng : lng, status : 'free', screen : Req.device_info, driver_id : record.id};
                        if(rows.length > 0)
                        {
                            qb.update('localtion', updateData, {driver_id:record.id}, (err, result) => {
                                if (err) return console.error(err);
                            });
                        }
                        else
                        {
                            qb.insert(model.DriverLogin, updateData, (err, res) => {
                                if (err) return console.error(err);
                            });
                        }
                    });
                });

                return res.json({"success" : true, "Message" : "Data Updated", "data" : Response});
            }
        }
    );
});
于 2017-12-04T11:46:30.960 回答