我有一个为安卓和iOS构建的PhoneGap应用程序的问题。在iOS和大多数安卓设备上,除了三星Galaxy S3和S4之外,它运行得很好。
在第一个页面加载时,应用程序会创建一个本地SQL数据库。此数据库用于存储整个应用程序中的问题和答案的值。
我在三星设备上遇到的问题是,第一次运行应用程序时,数据库将不会正确加载,但是,如果用户完全关闭应用程序并重新打开它,则创建数据库时不会出现错误。因为第一个页面要求用户输入他们的年龄,然后将值存储在SQL数据库中,所以用户会得到应用程序在这一点上冻结的印象。
数据库初始化代码:
index.html
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
populateDB();
}
main_js.js
function populateDB() {
var db = window.openDatabase("Database", "1.0", "My Database", 20000);
db.transaction(populate_DB, errorCB, successCB);
}
function populate_DB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS Options (ID INTEGER UNIQUE NOT NULL, Name TEXT NOT NULL, Value INTEGER NOT NULL)');
tx.executeSql('CREATE TABLE IF NOT EXISTS Questions (ID INTEGER UNIQUE NOT NULL, Question TEXT NOT NULL, Answer TEXT)');
tx.executeSql('INSERT OR IGNORE INTO Options (ID, Name, Value) VALUES (1, "License", 0)');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (0, "Age","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (1, "two","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (2, "three","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (3, "four","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (4, "five","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (5, "six","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (6, "seven","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (7, "eigth","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (8, "nine","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (9, "ten","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (10, "eleven","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (11, "twelve","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (12, "thirteen","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (13, "fourteen","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (14, "fifteen","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (15, "sixteen","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (16, "seventeen","")');
}
age_button_pressed
function age_save() {
var db = window.openDatabase("Database", "1.0", "My Database", 20000);
loc = "q_sex.html";
db.transaction(age_query, errorCB);
}
function age_query(tx) {
var age = document.getElementById("a_age").value;
tx.executeSql('UPDATE Questions SET Answer = "' + age + '" WHERE ID="0";', [], q_querySuccess, errorCB);
}
在age_button_pressed
之后,不会发生任何事情,数据库也不会更新结果。
这只是三星Galaxy S3和S4的问题,我想知道三星的WebKit是否与此有关?
发布于 2015-01-10 04:46:14
这可能是因为您正在打开数据库两次。只需打开它一次,然后在该实例上执行所有事务。另外,在开始另一个事务之前,还要确保您的前一个事务已完成。你不必担心这一点,但根据我的经验,科尔多瓦(PhoneGap)的WebSQL有点挑剔。
发布于 2015-04-15 15:20:12
我在三星和htc设备上使用phonegap 2.0到3.1版本,从来没有见过这样的问题。您使用的是哪个版本?尝试使用稳定版本,而不是测试版。
首先,有时phonegap应用程序在启动时会出现同步问题。也许这个问题可以与此相关。你可以试着把deviceready放在index.html里,在deviceready之后,把你的页面直接转到你的主页。在主页中执行数据库操作(如创建表和插入)。
其次,您需要跟踪errorCB函数上的错误消息。error函数获取一个参数,该参数包括errorcode和errormessage。看看这些。
最后,如果你使用的是android版本4.4的s4设备,你可以用chrome调试你的javascript代码。调试是phonegap的一大优势。使用它。
发布于 2016-03-08 19:45:05
我不认为这是设备的问题,它可能很忙。也许您可以尝试使用回调按顺序将每个订单执行到数据库:
BBDDD.transaction(
function(tx){
tx.executeSql("CREATE TABLE IF NOT EXISTS DateSession (\n\
id INTEGER PRIMARY KEY AUTOINCREMENT,\n\
field1 TEXT NOT NULL,\n\
field2 TEXT NOT NULL)");
},
function(err){
console.log("Error"+err.code);
},
function(){
BBDDD.transaction(function(tx){
tx.executeSql("CREATE TABLE IF NOT EXISTS Table2 (\n\
id INTEGER PRIMARY KEY AUTOINCREMENT,\n\
field1 TEXT NOT NULL,\n\
field2 TEXT NOT NULL)");
},
function(err){
console.log("Error"+err.code);
},
function(){
});
});
https://stackoverflow.com/questions/20059195
复制相似问题