saroten
saroten

Reputation: 95

using SQLite in android 4.4.2

I'm making an application for android using apache cordova tools for visual studio. But I'm facing a problem, when I execute my application on android 5.1.1+ everything works perfectly and when I run it on android 4.4.2 (can't try on lower version) there is an issue.

I've created an app to try to resolve the problem. I start by creating the Database using :

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

Then I create the table and try to retrieve the name of the table I've created :

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

And finally I display the data retrieved :

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

When I launch it on android 4.4.2 here is the error :

createTables : the statement callback raised an exception or statement error callback did not return false

This error comes from db.transaction in the function createTables that couldn't be executed properly.

I really don't understand why it works on android 5.1.1+ and not on 4.4.2(I've tried it on a smartphone and a tablet).

Upvotes: 1

Views: 503

Answers (1)

saroten
saroten

Reputation: 95

I've solved this problem by using results.rows.item(0).name instead of results.rows[0].name.

Upvotes: 1

Related Questions