Created
June 24, 2011 13:26
-
-
Save benpoole/1044759 to your computer and use it in GitHub Desktop.
Example 'factory' for initialising a Web SQL db connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Example 'factory' for initialising a Web SQL database connection | |
@author Ben Poole http://benpoole.com | |
@version 1.0.1 for Stack Overflow question: http://stackoverflow.com/questions/6284147 | |
*/ | |
// Sample transaction error callback | |
var errCallback = function(){ | |
alert("Error"); | |
}; | |
// Code to initialise your table(s) | |
var CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS some_table(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, somevalue TEXT, anothervalue TEXT, somenumber INTEGER);"; | |
// Prototype definition (requires jQuery) | |
window.DbConnection = (function($){ | |
function DbConnection(){ | |
this.conn = openDatabase('DbShortName', '1.0', 'Your database long name', 32678); | |
this.init(function() {}); | |
} | |
// A couple of methods | |
DbConnection.prototype = { | |
getDb: function(){return(this.conn);}, | |
init: function(successCallback){ | |
this.conn.transaction(function(transaction){ | |
transaction.executeSql(CREATE_TABLE_SQL, []); | |
}, | |
errCallback, successCallback); | |
}, | |
} | |
return(DbConnection); | |
})($); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment