Skip to content

Instantly share code, notes, and snippets.

@benpoole
Created June 24, 2011 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benpoole/1044759 to your computer and use it in GitHub Desktop.
Save benpoole/1044759 to your computer and use it in GitHub Desktop.
Example 'factory' for initialising a Web SQL db connection
/* 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