【问题标题】:using SQLite in android 4.4.2在 android 4.4.2 中使用 SQLite
【发布时间】:2016-01-21 11:17:17
【问题描述】:

我正在使用适用于 Visual Studio 的 apache cordova 工具为 android 制作应用程序。但是我遇到了一个问题,当我在 android 5.1.1+ 上执行我的应用程序时 一切正常,当我在 android 4.4.2 上运行它(不能在较低版本上尝试)时出现问题。

我创建了一个应用程序来尝试解决问题。我首先使用以下方法创建数据库:

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的成功方法。您只添加了错误方法。请尝试添加成功方法。你可以参考这个链接:stackoverflow.com/questions/33879785/…
  • 我照你说的做了,但也没用。
  • 你试过上面提到的链接吗?因为就我而言,它在 Android(所有版本)和适用于 Cordova 的 iOS 中运行良好。
  • 我几乎已经解决了这个问题。如果我使用选择查询删除该行,一切正常。它在初始化函数内部,执行查询时出现问题,但我不明白为什么。
  • 我认为您应该尝试在 SELECT 语句之后移动初始化函数。并从您的 CREATE TABLE 语句中删除 []。

标签: javascript android visual-studio android-sqlite apache-cordova


【解决方案1】:

我已经通过使用results.rows.item(0).name 而不是results.rows[0].name 解决了这个问题。

【讨论】:

    猜你喜欢
    • 2014-08-13
    • 1970-01-01
    • 2016-04-02
    • 2017-12-04
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多