[英]using SQLite in android 4.4.2
I'm making an application for android using apache cordova tools for visual studio. 我正在使用用于Visual Studio的Apache Cordova工具为android编写应用程序。 But I'm facing a problem, when I execute my application on android 5.1.1+ everything works perfectly and when I run it on android 4.4.2 (can't try on lower version) there is an issue. 但是我遇到了一个问题,当我在android 5.1.1+上执行我的应用程序时,一切工作正常,而当我在android 4.4.2上运行它时(不能尝试使用较低版本),出现了问题。
I've created an app to try to resolve the problem. 我创建了一个应用来尝试解决该问题。 I start by creating the Database using : 我首先使用创建数据库:
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();
}
Then I create the table and try to retrieve the name of the table I've created : 然后创建表并尝试检索已创建表的名称:
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;
});
}
);
}
And finally I display the data retrieved : 最后,我显示检索到的数据:
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;
});
}
}
When I launch it on android 4.4.2 here is the error : 当我在android 4.4.2上启动它时,这是错误:
createTables : the statement callback raised an exception or statement error callback did not return false
This error comes from db.transaction in the function createTables that couldn't be executed properly. 此错误来自无法正常执行的函数createTables中的db.transaction。
I really don't understand why it works on android 5.1.1+ and not on 4.4.2(I've tried it on a smartphone and a tablet). 我真的不明白为什么它可以在android 5.1.1+而不是4.4.2上运行(我已经在智能手机和平板电脑上尝试过了)。
我已经通过使用results.rows.item(0).name
而不是results.rows[0].name
解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.