javascript - Android phonegap 应用程序在三星 Galaxy 设备上存在 SQlite 和本地存储问题

标签 javascript android sql sqlite cordova

我遇到了针对 Android 和 iOS 构建的 PhoneGap 应用程序的问题。在 iOS 和大多数 Android 设备上,它运行良好,但三星 Galaxy S3 和 S4 除外。

在第一个页面加载时,应用程序会创建一个本地 SQL 数据库。该数据库用于存储整个应用程序中问题和答案的值。

我在三星设备上遇到的问题是,在第一次运行应用程序时数据库无法正确加载,但是如果用户完全关闭应用程序并重新打开它,则数据库创建时不会出现错误。因为第一页要求用户输入他们的年龄,然后将值存储在 SQL 数据库中,所以用户会得到应用程序此时已卡住的印象。

数据库初始化代码:

index.html

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    populateDB(); 
}

ma​​in_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 有点挑剔。

关于javascript - Android phonegap 应用程序在三星 Galaxy 设备上存在 SQlite 和本地存储问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20059195/

相关文章:

sql - 检查并更改 SQL 中列的空值或 null 值?

sql - 滚动每日不同计数

android - 如何在html段落中添加@string?

android - Phonegap 安卓 : Load local html from hosted application

php - 如何从多个表中获取按时间排序的最后 30 个项目?

javascript - 正则表达式构建字符串直到遇到第 N 个匹配项

javascript - 对象解构 ES6 类

javascript - 使用 knockoutjs 操作 View 模型

javascript - 用于开放图操作的访问 token

android - 在 android 中加载我的 Cordova 应用程序时显示 URL?