I am having a problem SELECTing all the rows in a small SQLite database, using node.js version 0.10.24 and the 'sqlite3' module from Node Packaged Modules (npmjs.org or 'npm'.) SQLite is version 3.8.2 compiled on OS X 10.9.1. The npm "sqlite3" module is version 2.1.19:
-----------------------------------------------------------
npm info sqlite3
{ name: 'sqlite3',
  description: 'Asynchronous, non-blocking SQLite3 bindings',
  'dist-tags': { latest: '2.1.19' },
-----------------------------------------------------------------------------

Here is my script:

var sqlite3 = require('sqlite3').verbose()
var async = require('async')
var myList = []
var db = new sqlite3.Database('./test2.db')
async.series([
// Create a table and populate it
    function (callback) {
db.run("CREATE TABLE lorem (listnumb bigint, info TEXT)", function (err) {
            if (err) return callback(err)
            console.log('Table created. ')
            callback()
        });

    },
    function (callback) {
stmt = db.prepare("INSERT INTO lorem VALUES (?, ?);", function (err) {
            if (err) return callback(err)
            for (var i = 0; i < 10; i++) {
                stmt.run(i, "Ipsum " + i)
            }
            stmt.finalize();
            console.log('Table populated. ')
            callback()
        })

    },
    function (callback) {
        db.each("SELECT listnumb as numb, info FROM lorem;",
            function (err, row) {
                if (err) return callback(err)
console.log(' numb = ' + row.numb + ' info field = ' + row.info)

            },
            function (err, cntx) {
                if (err) return callback(err)
                console.log('Number of retrieved rows is ' + cntx)
                callback()
            }

        )

    }
    ],
    // This function gets called after the tasks above have completed
    function(err) {
        if (err) return new Error(err)
console.log("\n\nLength of array after the function go runs " + myList.length)
        db.close()
    })
-------------------------------------------------------------------

The problem is that the db.each method shown above does not fetch all of the rows that table lorem in database test2.db has been populated with. I am running this script on OS X 10.9.1 in a terminal window. Here is sample output:

$ node pacheco3.js
Table created.
Table populated.
 numb = 0 info field = Ipsum 0
 numb = 1 info field = Ipsum 1
 numb = 2 info field = Ipsum 2
 numb = 3 info field = Ipsum 3
 numb = 4 info field = Ipsum 4
 numb = 5 info field = Ipsum 5
Number of retrieved rows is 6


Length of array after the function go runs 0

---------------------------------------------------------------------------------------

There should be 10 rows. Here is the content of the table:

$ sqlite3 test2.db
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select listnumb as numb, info from lorem;
0|Ipsum 0
1|Ipsum 1
2|Ipsum 2
3|Ipsum 3
4|Ipsum 4
5|Ipsum 5
6|Ipsum 6
7|Ipsum 7
8|Ipsum 8
9|Ipsum 9
sqlite> .exit

Is there a flaw in my node.js script? Or should I be using a different method of node-sqlite3 -- is there a method to be preferred over db.each? The API documentation doesn't really speak to this.

Thanks

Bob Cochran
Greenbelt, Maryland, USA




_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to