删除后的 SQLite 回调
SQLite callback after delete
我正在尝试创建删除功能并确认某些内容已被删除。如果未找到该行或已删除该行,则当前代码行变量返回空。
app.delete('/api/devices/:id', (req, res) => {
db.all('delete from devices where id = ' + req.params.id, (err, rows) =>
{
if (err) {
return res.status(500).send(err)
}
else {
return res.status(204).send()
}
})
})
万一找不到,我想return404,万一真的被删了,我想return204,怎么区分呢?
我知道我可以在删除它之前进行 select 查询,但必须有更好的方法。
In case the DELETE
statement executed successfully, the this
object of
the callback function will contain the changes property that stores
the number of rows deleted.
您应该考虑对 DELETE 查询使用 run
方法,而不是 all
方法,因为您不需要任何结果。
摘自 api doc
Runs the SQL query with the specified parameters and calls the
callback afterwards. It does not retrieve any result data.
...
If execution was successful, the this object will contain two
properties named lastID and changes which contain the value of the
last inserted row ID and the number of rows affected by this query
respectively. Note that lastID only contains valid information when
the query was a successfully completed INSERT statement and changes
only contains valid information when the query was a successfully
completed UPDATE or DELETE statement. In all other cases, the content
of these properties is inaccurate and should not be used. The .run()
function is the only query method that sets these two values; all
other query methods such as .all() or .get() don't retrieve these
values.
以防其他人在删除查询后点击此页面搜索 sqlite3 this.lastID
和 sqlite this.changes
return undefined
;
上面的代码片段应该是:
app.delete('/api/devices/:id', (req, res) => {
db.run('delete from devices where id = ?', req.params.id, function(err) {
if (err) return res.status(500).send(err)
return res.status(204).send(this.lastID) // or this.changes
})
})
OP 片段中缺少以下内容:
- 我们需要在数据库实例上执行
run
命令,而不是 all
,顺便说一句,内存效率低下...
- sqlite 查询缺少参数化变量(id)的问号
- 如果我们使用箭头函数,
this
上下文将丢失
run
方法回调只有一个参数,错误对象
我正在尝试创建删除功能并确认某些内容已被删除。如果未找到该行或已删除该行,则当前代码行变量返回空。
app.delete('/api/devices/:id', (req, res) => {
db.all('delete from devices where id = ' + req.params.id, (err, rows) =>
{
if (err) {
return res.status(500).send(err)
}
else {
return res.status(204).send()
}
})
})
万一找不到,我想return404,万一真的被删了,我想return204,怎么区分呢?
我知道我可以在删除它之前进行 select 查询,但必须有更好的方法。
In case the
DELETE
statement executed successfully, thethis
object of the callback function will contain the changes property that stores the number of rows deleted.
您应该考虑对 DELETE 查询使用 run
方法,而不是 all
方法,因为您不需要任何结果。
摘自 api doc
Runs the SQL query with the specified parameters and calls the callback afterwards. It does not retrieve any result data.
...
If execution was successful, the this object will contain two properties named lastID and changes which contain the value of the last inserted row ID and the number of rows affected by this query respectively. Note that lastID only contains valid information when the query was a successfully completed INSERT statement and changes only contains valid information when the query was a successfully completed UPDATE or DELETE statement. In all other cases, the content of these properties is inaccurate and should not be used. The .run() function is the only query method that sets these two values; all other query methods such as .all() or .get() don't retrieve these values.
以防其他人在删除查询后点击此页面搜索 sqlite3 this.lastID
和 sqlite this.changes
return undefined
;
上面的代码片段应该是:
app.delete('/api/devices/:id', (req, res) => {
db.run('delete from devices where id = ?', req.params.id, function(err) {
if (err) return res.status(500).send(err)
return res.status(204).send(this.lastID) // or this.changes
})
})
OP 片段中缺少以下内容:
- 我们需要在数据库实例上执行
run
命令,而不是all
,顺便说一句,内存效率低下... - sqlite 查询缺少参数化变量(id)的问号
- 如果我们使用箭头函数,
this
上下文将丢失 run
方法回调只有一个参数,错误对象