Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running sqlite3 query in node.js returns `undefined` rows with no error

Tags:

node.js

sqlite

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?

like image 806
Kof Avatar asked Oct 17 '25 23:10

Kof


1 Answers

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);
   });
});
like image 61
Andrius Avatar answered Oct 19 '25 13:10

Andrius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!