在 node.js 中运行 sqlite3 查询返回 `undefined` 行,没有错误

Kof*_*Kof 3 sqlite node.js

使用Node.js 4.2.1sqlite3 3.1.1下面的代码-

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

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

});
sqlitedb.close();
Run Code Online (Sandbox Code Playgroud)

undefined在控制台中打印,但如果使用该sqlite3工具执行相同的查询,它工作正常 -

$ 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">
...
Run Code Online (Sandbox Code Playgroud)

知道为什么 SQL 会undefined在 Node.js 中返回行吗?

And*_*ius 6

该方法run()不返回值,并且在回调中,仅在发生错误时返回错误。

要获取数据,您应该使用get()all()

这将完美地工作:

sqlitedb.serialize(function() {
   sqlitedb.get("SELECT ZDATA FROM ZMYTABLE;", function(err, rows) {
        console.log(err);
        console.log(rows);
   });
});
Run Code Online (Sandbox Code Playgroud)