我正在使用从npm安装的node-sqlite3。这个问题与该特定插件有关。我通过节点运行以下命令:
node select.js
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('foods.db');
db.serialize(function() {
db.each(
"select foods.name, food_types.name from foods, food_types where foods.type_id=food_types.id limit 10;"
, function(err, row){
if(err) {
console.log(err);
}
console.log(row);
});
})
db.close();
此代码返回
{ name: 'Bakery' }
{ name: 'Bakery' }
{ name: 'Bakery' }
{ name: 'Bakery' }
{ name: 'Bakery' }
{ name: 'Bakery' }
{ name: 'Bakery' }
{ name: 'Bakery' }
{ name: 'Bakery' }
{ name: 'Bakery' }
但是使用sqlite3
select foods.name, food_types.name from foods, food_types where foods.type_id=food_types.id limit 10;
返回(带.headers on,.mode列)
name name
---------- ----------
Bagels Bakery
Bagels, ra Bakery
Bavarian C Bakery
Bear Claws Bakery
Black and Bakery
Bread (wit Bakery
Butterfing Bakery
Carrot Cak Bakery
Chips Ahoy Bakery
Chocolate Bakery
我是否错误地使用了node-sqlite3 npm模块,我希望看到类似
的内容{ name: 'Bakery', name: 'Bagels' }
{ name: 'Bakery', name: 'Bagels, ra'}
{ name: 'Bakery', name: 'Bavarian C' }
{ name: 'Bakery', name: 'Bear Claws' }
等...
答案 0 :(得分:1)
SQLite模块无法使用相同的键两次返回结果对象;一个值必须覆盖另一个。您可以通过在SQL中使用as
为其中一列指定其他名称来解决此问题:
select foods.name as name_food, food_types.name as name_type from ...
以上将给出如下结果:
{ name_type: 'Bakery', name_food: 'Bagels' }
{ name_type: 'Bakery', name_food: 'Bagels, ra'}
{ name_type: 'Bakery', name_food: 'Bavarian C' }
{ name_type: 'Bakery', name_food: 'Bear Claws' }
...