[英]how do i SELECT then parse SQLite row values to assign as variables using Cordova-sqlite-storage plugin
我在Cordova中有一个多页应用程序,它使用一个(仅限于本地)SQLite数据库。 最初,我要做的就是使用一行存储2个不同的值,我偶尔希望对其进行选择,修改,然后再次更新该行。 我想我可以选择特定的一行,但是我不知道如何解析行中的两个值以准备分配给变量。
我正在使用cordova-sqlite-storage插件。
var myDB;
/first create/open the database
myDB = window.sqlitePlugin.openDatabase({ name: "mySQLite.db", location: 'default' });
//create table and populate
myDB.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS mcappDB (id text primary key, val1 integer, val2 integer)');
tx.executeSql('INSERT INTO mcappDB VALUES (?,?,?)', ['SessionNums', 12, 15]);
}, function (error) {
alert('Transaction ERROR: ' + error.message);
}, function () {
alert('Table created and populated');
});
//SELECT row and parse (and needing to store parsed data as global variables)
myDB.transaction(function (tx) {
tx.executeSql('SELECT * FROM mcappDB WHERE id="SessionNums"', [],
function (tx, rs) {
alert('Whats in here? ' + rs);
},
function (tx, error) {
alert('select ERROR: ' + error.message);
});
});
目前, rs
只是返回为[object Object]
而我无法对其进行解析。 我尝试使用rs.toString(0)
, rs.getString(0)
, rs.column.item(1)
, rs(1)
等无济于事。
实际上,我实际上是从WHERE id=SessionNums
开始的,即行ID上没有引号,但是奇怪的是,此返回的错误中no such columns: SessionNums
,我以为使用WHERE id=
命令就是为了获取行信息? 例如http://zetcode.com/db/sqlite/select/
任何和所有帮助将不胜感激。 干杯,克里斯。
首先,请确保您已插入记录,然后再用承诺阅读。
分别使用插入函数和读取函数,然后使用此函数:
$.when(insertFunc()).done(readFunc());
之后,您可以按以下方式分析结果:
if (rs.rows.length > 0) {
for (var i = 0; i < rs.rows.length; i++) {
console.log("val1: " + rs.rows.item(i).val1 + "\n" + "val2: " + rs.rows.item(i).val2);
}
}
最好的问候,@BernaAsis
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.