为什么我在javascript中的“UPDATE”SQLITE3不起作用?

oogrdqng  于 2023-05-18  发布在  SQLite
关注(0)|答案(1)|浏览(139)

所以我有这样的代码,但有时候这个.changes = 0,有时候这个.changes = 1。我不知道为什么在我的代码中,有db.run更新sqlite3数据库。有时this.changes为1,但有时this.changes为0。有人能帮我这个吗?变化总是1。

app.post("/like/:noteId", (req, res) => {
  //TODO first the shuf we will be false
  shuf = false;
  const noteId = parseInt(req.params.noteId.trim());
  
  //TODO next we will be search the position of noteId
  let itemIndex = -1;
  for (let i = 0; i < data.length; i++) {
    if (data[i].noteId == noteId) {
      itemIndex = i;
      break;
    }
  }

  if (itemIndex !== -1) {
    const item = data.splice(itemIndex, 1)[0];
    data.unshift(item);
    if (!item.hasLiked) {
      //TODO like usualy,the script will run the database first

      db.run("UPDATE data SET Like = Like + 1 WHERE noteId = ?", [noteId], function(err) {
        item.like++
        if (err) {
          return console.log(err.message);
        }
        console.log(`Row(s) updated: ${this.changes}`);
        item.hasLiked = true;
        res.cookie(`liked_${noteId}`, "true");
        res.redirect("/");
      });
    }
  }
});```
ctehm74n

ctehm74n1#

如果UPDATE语句中的WHERE子句不匹配表中的任何行,则它不更改任何行,因此在.changes中返回0。

相关问题