我正在尝试按照 tutorial 使用 NodeJS 从 SQLite 数据库中提取数据。在 @ShadowCode 回答之后,我修改了我的代码,如下所示。
const sqlite3 = require('sqlite3').verbose();
//open the database
let db = new sqlite3.Database('src/assets/academia.db');
db.run("SELECT userid as id, firstname, lastname FROM users", function(err, row){
console.log(row.userid + " " + row.firstname + " " + row.lastname);
});
//close connection
db.close();
我收到一条错误消息,指出它无法读取属性 userID。如果我在数据库中运行 sql 语句,它会完美运行。
TypeError: Cannot read property 'userid' of undefined
at Statement.<anonymous> (C:\academia_2\src\get.js:7:21)
--> in Database#run('SELECT userid, firstname, lastname FROM users', [Function])
最佳答案
也许你的教程有点过时了,也许看看文档 sqlite3 - npm .
看看这个片段,对我来说它有效。
db.run("UPDATE tbl SET name = ? WHERE id = ?", "bar", 2);
关于javascript - 尝试使用 NodeJS 从 SQLite DB 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54408477/