最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to SELECTget all rows with node-sqlite3? - Stack Overflow

programmeradmin0浏览0评论

I am trying to get all / more than one row of data out of sqlite3 database that I previously entered and ensured that it(data) is present. With db as the database object, my attempt looks like this:

 db.get
    (
        'SELECT * FROM my_table',
        (err, rows) =>
        {
            if(rows && err === null)
            {
                console.log(rows);
            }
            else
            {
                console.log('Error', err);
            }
        }
    )

Above always returns a single object with 1 row of data.

I am trying to get all / more than one row of data out of sqlite3 database that I previously entered and ensured that it(data) is present. With db as the database object, my attempt looks like this:

 db.get
    (
        'SELECT * FROM my_table',
        (err, rows) =>
        {
            if(rows && err === null)
            {
                console.log(rows);
            }
            else
            {
                console.log('Error', err);
            }
        }
    )

Above always returns a single object with 1 row of data.

Share Improve this question asked Aug 10, 2017 at 2:52 Robert C. HollandRobert C. Holland 1,8135 gold badges33 silver badges60 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 17

The issue here is that db.get() will only return the first row from the result set. From the documentation:

Runs the SQL query with the specified parameters and calls the callback with the first result row afterwards.

If you want to return the entire result set, use db.all() instead:

db.all("SELECT * FROM my_table", function(err, rows) {  
    rows.forEach(function (row) {  
        console.log(row.col1, row.col2);    // and other columns, if desired
    })  
});

You could also use db.each() here:

db.each("SELECT * FROM my_table", function(err, row) {
    console.log(row.col1, row.col2);    // and other columns, if desired
});
发布评论

评论列表(0)

  1. 暂无评论