首页 > 解决方案 > 在 SQLite3 中更新整数值

问题描述

有什么方法可以让这段 SQLite3 代码更短、更高效?我想将整数添加到列中的现有整数中。

我试过下面的代码。

(db 被定义为我的 SQLite3 db 连接)

db.all(`SELECT * FROM coins WHERE userId = ?`, '28978936', async (err, resp) => {

if (resp.length === 0) resp[0].balance = 0;

var earnings = resp[0].balance + 100;

  db.run(`INSERT INTO coins (userId, balance) VALUES (?, ?)`, '28978936', earnings, (err) => {

    console.log('100 coins have been added to user "28978936"')
  })
})

该代码对我有用,但是有更好的方法吗?

标签: javascriptnode.jsnode-sqlite3

解决方案


请执行以下查询

UPDATE coins SET balance = balance + 100 WHERE userId = ?

推荐阅读