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?
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);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With