3

I have a Phonegap (2.1.0) application that onDeviceready creates a DB and populates a table with info.

Running this locally (using the Ripple emulator) on Chrome works. Tables are being created and populated as required.

After installing the build .apk on my Android device my Eclipse logcat shows:

    sqlite returned: error code = 14, msg = cannot open file at line 27712 of [8609a15dfa], db=/data/data/<project>/databases/webview.db

    sqlite returned: error code = 14, msg = os_unix.c: open() at line 27712 - "" errno=2 path=/CachedGeoposition.db, db=/data/data/<project>/databases/webview.db

Which I believe according to this post here - can be ignored.

However - I also noticed this error in logcat:

    sqlite returned: error code = 1, msg = no such table: latest_events, db=/data/data/<project>/databases/webview.db

I have also - through adb shell - confirmed that the DB is not created: here: /data/data/com.application/databases. or here: /data/data/com.application/app_databases

So - my code:

 if (!window.openDatabase) {
        doMessage('Databases are not supported on this device. Sorry','error');
        return;
    }else{
        consoleLog('all good for storage');
        var db;
        var shortName = 'MyDB';
        var version = '1.0';
        var displayName = 'MyDB';
        var maxSize = 102400;

        function errorHandler(transaction, error) {consoleLog('Error: ' + error.message + ' code: ' + error.code);}

        function nullHandler(){};
        db = window.openDatabase(shortName, version, displayName,maxSize);


            consoleLog('starting table creation');
            db.transaction(function(tx){
                tx.executeSql( 'CREATE TABLE IF NOT EXISTS latest_events (id integer PRIMARY KEY AUTOINCREMENT,EventID integer,EventLocation text,EventName text,EventDateFrom varchar,EventTime timestamp,EventPresentedBy varchar,EventVenue varchar,EventScript text,RequireRSVP varchar)',[],nullHandler,errorHandler);
                db.transaction(function(tx){
                    tx.executeSql('SELECT count(id) as RowCount FROM device_info ', [],
                        function(tx, result) {
                            if (result != null && result.rows != null) {
                                for (var i = 0; i < result.rows.length; i++) {
                                    var row = result.rows.item(i);
                                    consoleLog('rowcount: '+row.RowCount);
                                    if(row.RowCount==0){
                                        tx.executeSql('INSERT INTO device_info (device_name, device_platform, device_uuid, device_os_ver, date_last_used) VALUES (?,?,?,?,?)',[device.name, device.platform, device.uuid, device.version, window.bowman_config.siteDate],nullHandler,errorHandler);
                                        //doMessage('device info row added','notice');
                                    }
                                }
                            }
                        },errorHandler);
                },errorHandler,successCallBack('2'));
                //doMessage('device info row added','notice');
            },errorHandler,successCallBack('1'));
}

To add to my woes - on my logcat I do see the console.log output for "all good for storage", and the "starting table creation" messages.

My errorHandler functions are not returning anything and my successCallBack functions are triggered...but no DB created.

Thanks for the help.

Community
  • 1
  • 1
DavidP
  • 1,788
  • 1
  • 15
  • 23

3 Answers3

0

When you pass in successCallBack("2") and successCallBack("1") then you are actually invoking them directly so you may be getting false positives on whether or not success has actually been called. You should provide two separate success call backs or just in-line some functions that call console.log("1") for instance.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • Thanks for the suggestion Simon. I have added inline console.log('1') and even un-commented the doMessage function - they fire. But no DB created. – DavidP Nov 13 '12 at 19:40
  • Weird, I'd try simplifying the create statement and take it from there. – Simon MacDonald Nov 14 '12 at 02:01
  • I have honestly tried it as simple as possible. Will try another suggestion and revert. Thanks for the feedback. – DavidP Nov 20 '12 at 09:10
0

Today this issue cost me 3 hours. What I tried:

  • Rewriting the copy database code.
  • Deleting the app from the emulator / device
  • Wiping emulator(s)
  • Cleaning eclipse
  • Changing file permissions
  • Validate a working SQLite database file

I solved the problem by copying the code from a shared Dropbox account to another location and refactoring the code in the Android Manifest and java files with another package name.

The application runs beautifully now, i.e. nothing wrong with the code, but somewhere it's muxed up by Dropbox.

artens
  • 61
  • 1
  • 4
0

I broke the nested functions up into single functions and 'chained' them based on their success or fail. It's actually been a lot simpler than I thought. RTFM it seemed. Thanks for all the help.

simplified:

var db = window.openDatabase("TheApp", "1.0", "The App", 50000000);
db.transaction(queryDB, errorCB, successCB);

// Query the database   //
function queryDB(tx) {
    //tx.executeSql('SELECT * FROM table", [], querySuccess, errorCB);
}

function querySuccess(tx, results) {
    //do more functions here
}

function errorCB(err) {
    console.log("Error processing SQL: "+err.code);
}
DavidP
  • 1,788
  • 1
  • 15
  • 23