Electron Js 和 SQLite3 不返回最后一个 ID
Elctronjs and SQLite 3 not returning last id
我正在使用 sqlite 3 数据库开发电子应用程序。我想在数据库中保存数据后获取最后插入的 ID。
我有 运行 下面的查询,但调用结果总是显示未定义。谁能帮我解决这个问题?
db.run(`INSERT INTO myTable (data1, data2) VALUES (aa, bb)`, (err) => {
console.log(this.lastID)
if (err) {
console.log(err)
} else {
console.log( this.lastID)
}
数据库插入成功,但结果,this.lastID结果未定义。
这是因为你使用了箭头语法。箭头语法将 this
上下文设置为父级。你应该这样做:
db.run(`INSERT INTO myTable (data1, data2) VALUES (aa, bb)`, function (err) {
console.log(this.lastID)
if (err) {
console.log(err)
} else {
console.log( this.lastID)
})
The call, apply and bind methods are NOT suitable for Arrow functions -- as they were designed to allow methods to execute within different scopes -- because Arrow functions establish "this" based on the scope the Arrow function is defined within.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
我正在使用 sqlite 3 数据库开发电子应用程序。我想在数据库中保存数据后获取最后插入的 ID。
我有 运行 下面的查询,但调用结果总是显示未定义。谁能帮我解决这个问题?
db.run(`INSERT INTO myTable (data1, data2) VALUES (aa, bb)`, (err) => {
console.log(this.lastID)
if (err) {
console.log(err)
} else {
console.log( this.lastID)
}
数据库插入成功,但结果,this.lastID结果未定义。
这是因为你使用了箭头语法。箭头语法将 this
上下文设置为父级。你应该这样做:
db.run(`INSERT INTO myTable (data1, data2) VALUES (aa, bb)`, function (err) {
console.log(this.lastID)
if (err) {
console.log(err)
} else {
console.log( this.lastID)
})
The call, apply and bind methods are NOT suitable for Arrow functions -- as they were designed to allow methods to execute within different scopes -- because Arrow functions establish "this" based on the scope the Arrow function is defined within.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions