javascript - 在 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"')
})
})
该代码对我有用,但是有更好的方法吗?
解决方案
请执行以下查询
UPDATE coins SET balance = balance + 100 WHERE userId = ?
推荐阅读
- python - webdriver 找不到带有 jscontroller 的显示更多链接
- python - 为什么 asyncio 需要一个特殊的信号处理程序?
- asp.net - 用户名或密码不正确
- sql - SQL Server 2012 消息“CREATE VIEW”必须是查询批处理中的第一条语句
- error-handling - SBCL 绕过调试器并打印错误以在其他环境中显示?
- .net-core - .NET Core 2.2 向 ajax 返回异步 SendGrid 响应
- python - 为什么 rstrip() 不删除尾随空格?
- datatables - 如何修复多个页码并在数据表中搜索?当我单击时,它会保持加载额外的页码和搜索(请参阅下面的详细信息))
- pyspark - 当列名为空白时如何使用 pyspark 过滤器
- algorithm - 像彩虹一样对颜色进行排序的算法