0

I'm breaking my bigger problem into a few bitesize chunks to resolve. I'm practicing Javascript/SQL interactions - and failing at the third hurdle, being inserting data:

var databaseOptions = {
    fileName: "sqlite_WAtest",
    version: "1.0",
    displayName: "SQLite WA Test",
    maxSize: 1024
}; // End databaseOptions



var database = openDatabase(
    databaseOptions.fileName,
    databaseOptions.version,
    databaseOptions.displayName,
    databaseOptions.maxSize
); // End database



database.transaction(
    function( transaction ){

        // Create our table if it doesn't exist
        transaction.executeSql(
            "CREATE TABLE IF NOT EXISTS WORKOUTS (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, WOdate, WOtype);");

    },function(){
        alert('error Creating!');
    },function(){
        alert('success Creating');
    }
); // end database.transaction



// Try and insert dummy info into table
database.transaction(
    function( transaction ){

        // Insert the data
        transaction.executeSql('INSERT INTO WORKOUTS (WOdate, WOtype) VALUES ("Test 1","Test 2")');

    },function(){
        alert('error inserting!');
    },function(){
        alert('success inserting');
    }
); // end database.transaction

When I load this page the alert fires saying "success creating!" - good news.

However I then get "error inserting" = not so good news.

The error is that there is no column named WOdate in the table WORKOUTS.

I've re-written the INSERT code a few times but I can't get it to work how I would expect it to. I'm tracking the table in the Safari Storage window and no data is getting into the table.

Can anyone shed some light on this?

EDIT: The issue was I already had a table in place without the correct columns. I have added this for the purpose of my experiment which made the code work:

   // Delete the table before running - because I'm testing
    // This is needed
    //
    database.transaction(
        function( transaction ){

            // Drop table
            transaction.executeSql(
                "DROP TABLE WORKOUTS");

        },function(err){
            alert('error Dropping: ' + err.code + '; Message: ' + err.message);
        },function(){
            alert('success Dropping');
        }
    ); // end database.transaction

Two lessons learned for me here: 1) Error call back handling - make use of the .code and .message properties and flag these. Helps narrow down the problem. 2) Be wary of existing data when testing/building

8
  • There must be a way to check the exact error message, error inserting says nothing about what's happened. Commented Mar 13, 2016 at 21:38
  • 1
    Don't you need to pass two or more params when doing an insert? The example inserts I've seen look like transaction.executeSql('insert into people (name, shirt) VALUES ("Joe", "Green");', [], nullDataHandler, errorHandler); Commented Mar 13, 2016 at 21:39
  • I'd assume that the error handler function gets passed a parameter which has a description of the error somewhere. Commented Mar 13, 2016 at 21:42
  • Related question: Issue with insert query in Sqlite Commented Mar 13, 2016 at 22:00
  • 1
    I've added a bit more detail on the error - The error is that there is no column named WOdate in the table WORKOUTS. Commented Mar 13, 2016 at 22:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.