2

I'm currently having a problem where when I try to insert data I'm receiving an error that the table column does not exist but I have defined it. I've currently isolated the problem and decided to insert data manually. Once I get past my alert('inserting data manually'), a popup shows with Error was could not prepare statement (1 table expenses has no column named expenseDate) (Code 5).

I have taken these SQL statements and tested them in a SQLite3 database, and everything checked out fine. The interesting thing is that when I go to my developer console->resources tab, I do not see my tables.

Any help would be appreciated.

My js is below:

var shortName='TravelExpenseReporting';
var version='0.1';
var displayName='TravelExpenseReporting';
var maxSize = 65536;

db = openDatabase(shortName,version,displayName,maxSize);
try {
 db.transaction(
    function(transaction) {
        transaction.executeSql(
            'CREATE TABLE IF NOT EXISTS expenses ' +
            '(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' +
            'type TEXT NOT NULL, ' + 'amount DOUBLE NOT NULL, ' +
            'vendor TEXT NOT NULL, ' + 'expensedate DATE NOT NULL);' 
        );
    }
 ); // END transaction()
} catch(e) {
alert('error: ' + e);
return;
}

alert('inserting data manually');

try {
 db.transaction(
    function(transaction) {
        transaction.executeSql(
            'INSERT INTO expenses (type,amount,vendor,expensedate) VALUES (?,?,?,?);',
            ['expenseType',100,'vendor','01/01/2004'],
            null,
            errorHandler
        );
    }
 ); // END transaction()
} catch(e) {
alert('error: ' + e);
return;

}

Richard
  • 826
  • 7
  • 23
  • 41

1 Answers1

0

The problem is that you don't handle correctly errors. You shouldn't use a try/catch to get errors. In fact, the transaction() function is asynchronous : it means that if that the program continue to run whereas the transaction() function didn't finish its work. So, when you see the alert "inserting data manually", the transaction() isn't supposed to have finished.

The transaction method take 2 function parameter : one executed when the request succeed, one when the request failed :

 db.transaction(
    function(transaction) {
        // success
        // put your requests
    },
    function(error) {
        // error
        System.out.println(error);
    }
 );

So now, you'll be able to see the errors.

As the transaction() method is asynchronous, you should pay attention that the insert requests is not executed before your creation table request. So you should put all your requests in the same transaction :

 db.transaction(
    function(transaction) {
        //create table
        //insert
    },
    function(error) {
        System.out.println(error);
    }
 );
GuillaumeP
  • 504
  • 5
  • 19
  • Thanks for your input. I've fixed my error handling, but the problem still exists. – Richard Jun 03 '14 at 16:06
  • 1
    It seems that the table is not created if you don't see it in the console>resources. When you make the create request, Is it the success function or the error function which is executed ? – GuillaumeP Jun 04 '14 at 07:22