我正在使用 Visual Studio 的 apache cordova 工具制作 Android 应用程序。但是当我在 android 5.1.1+ 上执行我的应用程序时,我遇到了一个问题 一切工作正常,当我在 android 4.4.2 上运行它时(无法尝试较低版本),出现了问题。
我创建了一个应用程序来尝试解决该问题。我首先使用以下命令创建数据库:
function openDB() {
try {
if (!window.openDatabase) {
$("#error").text("Not supported");
} else {
var shortName = 'TestDataBase20';
var version = '1.0';
var displayName = 'TestDataBase';
var maxSize = 2048; // in bytes
db = window.openDatabase(shortName, version, displayName, maxSize);
}
} catch (e) {
// Error handling code goes here.
if (e == 2) {
// Version number mismatch.
$("#error").text("Invalid database version.");
} else {
$("#error").text("Unknown error " + e + ".");
}
return;
}
createTables();
}
然后我创建表并尝试检索我创建的表的名称:
function createTables() {
db.transaction(
function (transaction) {
/* The first query causes the transaction to (intentionally) fail if the table exists. */
transaction.executeSql('CREATE TABLE IF NOT EXISTS settings (id INTEGER PRIMARY KEY AUTOINCREMENT, login TEXT NOT NULL);', [], nullDataHandler, errorHandler);
/*Select the name of the table called settings*/
transaction.executeSql('SELECT name FROM sqlite_master WHERE type="table" AND name="settings";', [], initialize, errorHandler);
},
/*Executed if there is a problem during the execution of db.transaction*/
function(error){
$("#error").text(function () {
var text = $("#error").text() + " createTables : " + error.message;
return text;
});
}
);
}
最后我显示检索到的数据:
function initialize(transaction, results) {
if (results.rows.length != 0) {
$("#result").text(function () {
var text = $("#result").text() + " " + results.rows[0].name;
return text;
});
} else {
$("#result").text(function () {
var text = $("#result").text() + " No elements in results";
return text;
});
}
}
当我在 android 4.4.2 上启动它时,出现错误:
createTables : the statement callback raised an exception or statement error callback did not return false
此错误来自createTables函数中的db.transaction无法正确执行。
我真的不明白为什么它可以在 android 5.1.1+ 上运行,而不能在 4.4.2 上运行(我已经在智能手机和平板电脑上尝试过了)。
最佳答案
我通过使用 results.rows.item(0).name
而不是 results.rows[0].name
解决了这个问题。
关于javascript - 在android 4.4.2中使用SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34922340/