Andrew Rhyne
Andrew Rhyne

Reputation: 5090

PhoneGap + SQLite Issues

For some reason, the following code doesn't work:

var users = window.SQLitePlugin.openDatabase("users","1.0","Demo",-1);
users.transaction(function(tx){
    KovarApp.lib.say("Transaction Started");
    var createString =  "CREATE TABLE IF NOT EXISTS 'users' ('id' integer(11) primary key, 'username' varchar(25),'password' varchar(100),'firstname' varchar(25),'lastname' varchar(25),'qdatabase' varchar(20),'ugroup' varchar(50),'status' varchar(50))";
    tx.executeSql(createString,[],function(tx,res){
        KovarApp.lib.say("Create Worked");
    },function(){
        KovarApp.lib.say("Create Failed");
    });

    var insertString = "INSERT INTO users (username,password,firstname,lastname,qdatabase,ugroup,status) values ('jktest','e10adc3949ba59abbe56e057f20f883e','Andrew','Rhyne','basictemplate','admin','yes')";
    tx.executeSql(insertString,[],function(tx,res){
        KovarApp.lib.say("Insert Worked");
    },function(){
        KovarApp.lib.say("Insert Failed");
    });

    var testString = "SELECT 1 FROM users WHERE username = 'jktest'";
    tx.executeSql(testString,[],function(tx,res){
        KovarApp.lib.say("res.rows.length: " + res.rows.length + " -- should be 1");
    });
    KovarApp.lib.say('Transaction Ended');
});

My log function spits out transaction started and transaction ended, but the callbacks aren't firing for the individual queries. I am using Brody's sqlite3 plugin: https://github.com/brodyspark/PhoneGap-SQLitePlugin-Android

Any help here would be awesome. I don't really know what I am doing wrong but it isn't working at all. As a side note, he has specified that there isn't any limit on database size, and this is denoted by the -1 (meaning no size limit).

Upvotes: 1

Views: 918

Answers (2)

Andrew Rhyne
Andrew Rhyne

Reputation: 5090

The Javascript files within Brody's project are platform-dependent for the time being. It looks like there will eventually be abstraction in place where the javascript is all portable, but this was the issue - I had the iOS version of the JS file running on Android. Duhh

Upvotes: 0

CL.
CL.

Reputation: 180070

All callback functions are executed asynchronously. In your code, the insert/select commands are started before the previous commands have finished, and the transaction ends before the individual commands have finished.

Move the code to start a command into the success callback function of the previous command.
In other words, the transaction and executeSql calls should always be the last statement in their containing function.

Upvotes: 2

Related Questions