我使用 nativescript-sqlite 插件已经有一段时间了。我正在从工作正常的表中选择数据。我想通过表的列名访问这些数据,这就是挑战开始的地方。
viewModel.selectAllRecords = function () {
new sqlite("etudiz.db", function (err, db) {
db.all("SELECT post_title,post_cat FROM posts ORDER BY id", [], function (tableErr, rs) {
var ObjLength = rs.length;
var i = 0;
for(i >= 0; i<ObjLength; i++) {
console.log(rs[i]['post_title']) //This is undefined
console.log(rs[i][0])//This is working well
}
});
});
};
我更喜欢按列名读取数据,而不是 0,1,2...等。是否有解决方法或者 SQLite 就是这样设计的?
最佳答案
DB.resultType()
Parameters
- Pass in
Sqlite.RESULTSASOBJECT
orSqlite.RESULTSASARRAY
to change the result sets configuration This will set the database to return the results in which ever choice you make. (Default isRESULTSASARRAY
)
Sqlite.RESULTSASARRAY
- Returns results as Arrays (ex:select name, phone
--- results[[name,phone]]
)
Sqlite.RESULTSASOBJECT
- Returns results as Objects (ex:select name, phone
--- results[{name: name, phone: phone}]
关于javascript - SQLite Nativescript中按列名读取查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46751768/