为什么我的select语句在可用的update语句后返回旧数据?

时间:2018-12-18 04:55:27

标签: node.js sqlite

我在快递服务器上运行以下代码。我的目标是更新数据库,然后查询更新的表。一切正常,除了查询不返回更新的结果。如果我第二次运行它,它将为第一次更新返回正确的结果。

        //// Run Update
        var db = new sqlite3.Database(myFile);
        var UpdateStatement = "UPDATE Employees SET Firstname = '"+Firstname+"', Lastname = '"+Lastname+"' WHERE id = "+ID+";";
        db.run(UpdateStatement);    
        db.close();

        //// Query table    
        var SELECTSTATEMENT = "SELECT * FROM Employees ;";  
        db = new sqlite3.Database(scheduleFile);        
        db.all(SELECTSTATEMENT,[],(err, rows) => {  if (err) { console.log("error at get /db.all Select  "); throw err; }   
            var strArr = JSON.stringify(rows);
            response.send(strArr);// Does not send back updated results.            
            db.close();
        });

即使我创建了新变量,我猜数据库变量仍然过时吗?

1 个答案:

答案 0 :(得分:1)

//create sql statement and bind the parameters
var db = new sqlite3.Database(myFile);
var UpdateStatement = "UPDATE Employees SET Firstname = ?, Lastname = ? WHERE id = ?;";


// run statement and get the callback once its done
db.run(UpdateStatement,[firstName, lastName, id], function(err){
     // if err occured return the error
      if(err)
        return err
     // else return the response
      var SELECTSTATEMENT = "SELECT * FROM Employees ;";  
            db = new sqlite3.Database(scheduleFile);        
            db.all(SELECTSTATEMENT,[],(err, rows) => {  if (err) { console.log("error at get /db.all Select  "); throw err; }   
                var strArr = JSON.stringify(rows);
                response.send(strArr);// Does not send back updated results.            
                db.close();
            });

    });