Unable to Obtain All Rows In A Table with database#each
In this script:
var sqlite3 = require('sqlite3').verbose()
var async = require('async')
var myList = []
var db = new sqlite3.Database('./test0.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 db.each method does not obtain all the rows in the actual table. On my machine, an OS X 10.9.1 MacBook Pro, using node.js v0.10.24 and async 2.1.19, I get the following 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
If I query the table directly using the sqlite3 shell, I get this output:
$ sqlite3 test0.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
I am very green at coding node.js and JavaScript, so perhaps this problem is a result of my own poor coding? Or is it a bug?
Thanks,
Bob
there is something wrong with your post and your code example appears to be incomplete.
cheers
Hi! Thank you for letting me know. I have never opened a GitHub "issue" before. I will try to attach the code example as a file later this evening Eastern time.
Thanks!
Bob Cochran
On Jan 13, 2014, at 7:08 AM, feichh [email protected] wrote:
there is something wrong with your post and your code example appears to be incomplete.
cheers
— Reply to this email directly or view it on GitHub.
I apologize for my mistakes in attempting to show the code that I am using to implement db.each. I will try again to show the code.
var sqlite3 = require('sqlite3').verbose()
var async = require('async')
var myList = []
var db = new sqlite3.Database('./test0.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()
})
I hope this helps. To repeat the issue:
db.each does not seem to return every row in the table, as the query should.
Thanks, Bob Cochran.
fwiw after >1week: arent the stmt.run calls in your second function asynchronous? it looks like the select gets executed simultaneously and runs b4 all inserts are completed.
cheers.
Thank you for getting back to me again about this. In the code example given above, I am using async.series (e.g. the 'async' module) which is supposed to wait for each of the functions in the code example given above to complete, before going on to execute the next subsequent function in the array of functions passed to it. So as you say the stmt.run is asynchronous. However my understanding is that async.series will not call the next function in this list (my third function which queries the database) until the inserts have all completed.
With all that said, I admit I am very inexperienced with node.js and asynchronous programming. Perhaps I need to use some different way to asynchrnously insert the rows and then read them back. I could be doing it all wrong.
im not an expert on node.js either. that said, i did test it myself: run a second program only executing the SELECT after this one completes, to see what actually ends up in the DB. your second function does not wait for the stmt.run calls, therefore it does not matter that async waits for the function, if that makes sense. I dont know how async works, but im pretty sure the issue is not with node-sqlite3.
cheers.
Thank you for checking into this. It seems I have more to learn. I will experiment with my code more.
Noticed that no solution to this, but the most probable solution to that stmt.finalize(); needs to have calllback, otherwise it goes to next async.series before it finishes.
The example code on README.md is not the best example how to do stmt.finalize();