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
error insertingsays nothing about what's happened.transaction.executeSql('insert into people (name, shirt) VALUES ("Joe", "Green");', [], nullDataHandler, errorHandler);