我正在尝试从我之前输入的 sqlite3 数据库中获取所有/不止一行数据,并确保它(数据)存在。使用 db
作为数据库对象,我的尝试如下所示:
db.get
(
'SELECT * FROM my_table',
(err, rows) =>
{
if(rows && err === null)
{
console.log(rows);
}
else
{
console.log('Error', err);
}
}
)
上面总是返回一个包含 1 行数据的对象。
最佳答案
这里的问题是 db.get()
只会返回结果集中的第一行。来自documentation :
Runs the SQL query with the specified parameters and calls the callback with the first result row afterwards.
如果要返回整个结果集,请改用db.all()
:
db.all("SELECT * FROM my_table", function(err, rows) {
rows.forEach(function (row) {
console.log(row.col1, row.col2); // and other columns, if desired
})
});
你也可以在这里使用 db.each()
:
db.each("SELECT * FROM my_table", function(err, row) {
console.log(row.col1, row.col2); // and other columns, if desired
});
关于javascript - 如何使用 node-sqlite3 选择/获取所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45603651/