Rob*_*and 1 javascript node.js node-sqlite3
我试图从我之前输入的sqlite3数据库中获取所有/多行数据,并确保它(数据)存在.使用db数据库对象,我的尝试看起来像这样:
db.get
(
'SELECT * FROM my_table',
(err, rows) =>
{
if(rows && err === null)
{
console.log(rows);
}
else
{
console.log('Error', err);
}
}
)
Run Code Online (Sandbox Code Playgroud)
以上始终返回包含1行数据的单个对象.
这里的问题是db.get()只返回结果集中的第一行.从文档:
使用指定的参数运行SQL查询,然后使用第一个结果行调用回调.
如果要返回整个结果集,请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
})
});
Run Code Online (Sandbox Code Playgroud)
你也可以db.each()在这里使用:
db.each("SELECT * FROM my_table", function(err, row) {
console.log(row.col1, row.col2); // and other columns, if desired
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4073 次 |
| 最近记录: |