安卓phonegap应用程序与三星Galaxy设备上的SQlite和本地存储存在问题

Android phonegap application having issues with SQlite and local storage on Samsung Galaxy devices

本文关键字:SQlite 存储 问题 存在 应用程序 phonegap 三星 Galaxy 安卓      更新时间:2023-09-26

我有一个为Android和iOS构建的PhoneGap应用程序出现问题。在iOS和大多数Android设备上,除了三星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是否与此有关?

这可能是因为您要打开数据库两次。只打开一次,并在该实例上执行所有事务。在开始另一笔交易之前,还要确保您的上一笔交易已经完成。你不必担心,但根据我的经验,Cordova的(PhoneGap)WebSQL有点挑剔。

我在三星和htc设备的2.0到3.1版本之前一直使用phonegap,从未见过这样的问题。您正在使用哪个版本?尽量使用稳定版本,而不是测试版。

首先,有时phonegap应用程序在启动时可能会出现同步问题。也许这个问题与此有关。你可以试着把deviceready放在index.html中,在deviceready之后,把你的页面指向主页。在主页面中执行数据库操作(如创建表和插入)。

其次,您需要跟踪errorCB函数上的错误消息。error函数获取一个参数,该参数包括errorcode和errormessage。看看这些。

最后,如果你使用的是安卓4.4版的s4设备,你可以用chrome调试你的javascript代码。调试是phonegap的一大优势。使用它。

我不认为这是设备的问题,它可能很忙。也许您可以尝试使用回调按顺序执行数据库的每个订单:

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(){
      });
  });

尝试将数据库大小从20000 增加到200000

var db=window.openDatabase("数据库","1.0","我的数据库",200000);