4

Using Node.js 4.2.1 with sqlite3 3.1.1 The following code -

var sqlitedb = new sqlite3.Database(sqliteFilename);
sqlitedb.serialize(function() {

   sqlitedb.run("SELECT ZDATA FROM ZMYTABLE;", function(err, rows) {
      console.log(rows);
   });

});
sqlitedb.close();

Prints undefined in the console, but if the same query is executed using the sqlite3 tool, it works fine -

$ sqlite3 backup.sqlite 
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite> SELECT ZDATA FROM ZMYTABLE;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
...

Any idea why SQL returns undefined rows in Node.js?

3
  • Have you checked if the code returns an error? Commented Nov 10, 2015 at 18:34
  • Have you tried removing the semicolon from the query string? I wouldn't expect it to cause an error, but it shouldn't need to be there. Commented Nov 10, 2015 at 22:07
  • Can you include some of your table data and structure so it's testable? Commented Nov 11, 2015 at 7:11

2 Answers 2

9
+50

The method run() does not return a value and in the callback, only returns an error if one occurred.

To fetch data, you should use get() or all().

This will work perfectly:

sqlitedb.serialize(function() {
   sqlitedb.get("SELECT ZDATA FROM ZMYTABLE;", function(err, rows) {
        console.log(err);
        console.log(rows);
   });
});
Sign up to request clarification or add additional context in comments.

Comments

0

sqlite3 3.1.1 is not supporting Node.js 5.0.0. We should wait update of sqlite3.

2 Comments

It also happens in Node.js 4.2.1.
The sqlite3 module works with Node.js v0.10.x, v0.12.x, v0.4.x, and v0.5.x.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.