簡體   English   中英

更新node.js中的查詢

[英]Update query in node.js

我必須在數據庫中更新一行並將其寫入

var o_voto_foto = {
    Profilo: profilo,
    Foto: data.id_foto_votata,
    punteggio: data.voto,
};

connection.query('UPDATE prof_voto_foto SET ? WHERE profilo_id = :Profilo AND foto_id = :Foto',o_voto_foto, function(error, rows) {
    if (error) {
        var err = "Error on UPDATE 'votoFotoFancybox': " + error;
        console.error(err);
        throw err;
    }
    connection.end();

    callback();

});

但出現語法錯誤。 但是,如果查詢使用此語法完成

connection.query('UPDATE prof_voto_foto SET punteggio = '+data.voto+' WHERE profilo_id = '+profilo+' AND foto_id = '+data.id_foto_votata,function(error, rows) {

我使用這個外部庫https://github.com/felixge/node-mysql

這樣做肯定會起作用:

var o_voto_foto = {
    Profilo: profilo,
    Foto: data.id_foto_votata,
    punteggio: data.voto,
};

var params = [o_voto_foto.punteggio, o_voto_foto.Profilo, o_voto_foto.Foto ];

connection.query('UPDATE prof_voto_foto SET punteggio = ? WHERE profilo_id = ? AND foto_id = ? ', params, function(error, rows) {

僅當您注冊自定義queryFormat時,才能替換以冒號開頭的,與參數對象中的條目匹配的查詢中的參數。 請參見下面的示例。 你不能混嗎? 和參數以冒號開頭。

connection.config.queryFormat = function (query, values) {
  if (!values) return query;
  return query.replace(/\:(\w+)/g, function (txt, key) {
    if (values.hasOwnProperty(key)) {
      return this.escape(values[key]);
    }
    return txt;
  }.bind(this));
};

var params2 = {
punteggio : '',
profilo_id : '',
photo_id : ''
};

connection.query('UPDATE prof_voto_foto SET punteggio = :punteggio WHERE profilo_id = :profilo_id AND foto_id = :photo_id ', params2, function(error, rows) {

o_voto_foto是一個對象。 轉義符查找數組。 嘗試使用[o_voto_foto]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM