0
我正在为使用apache cordova工具进行视觉工作室的android应用程序。但我遇到了一个问题,当我在android 5.1.1+ 上执行我的应用程序时,一切都很完美,当我在android 4.4.2上运行它时(无法在较低版本上尝试)出现问题。在Android 4.4.2中使用SQLite
我创建了一个应用程序来尝试解决问题。我开始通过创建数据库:
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上(我在智能手机和平板电脑上试过)。
我想你还没有添加db.transaction的成功方法。您只添加了错误方法。请尝试添加成功方法。你可以参考这个链接:http://stackoverflow.com/questions/33879785/cordova-sqlite-plugin-not-functioning-with-android-studio/33894275#33894275 – Dhruv
我照你说的做,但它也不工作。 – saroten
你有没有尝试上面提到的链接?因为就我而言,它在Android(所有版本)和Cordova的iOS上都非常完美。 – Dhruv