javascript - 在android 4.4.2中使用SQLite

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

我正在使用 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 上运行(我已经在智能手机和平板电脑上尝试过了)。

最佳答案

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

关于javascript - 在android 4.4.2中使用SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34922340/

相关文章:

android - 如何在Android中更改开关的文本颜色

android - 如何在 google maps API for android 中为自动完成预测添加样式?

java - Android - 找不到默认 Activity

visual-studio - 托管调试助手 'FatalExecutionEngineError' 0xc0000005

c++ - 如何最好地为 Visual Studio 2017 构建的 CMake C++ 项目设置输出目录?

javascript - AngularJS $resource 将 id 作为查询参数而不是 url 传递

javascript - 将鼠标滚轮滚动添加到 Konva JS Stage,并在最外层对象的开头和结尾处使用 anchor

javascript - 将数据导入 HTML 文件中的脚本的最简单方法

javascript - jQuery 和 typeahead 上的自动完成困难

c++ - 使用 Visual C++ 2015 工具集构建 Windows/Windows Phone 8.1