Skip to content

Instantly share code, notes, and snippets.

@buzzedword
Created September 26, 2011 23:03
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 buzzedword/1243690 to your computer and use it in GitHub Desktop.
Save buzzedword/1243690 to your computer and use it in GitHub Desktop.
Messing around with Web SQL API
(function($, undefined){
var ns = {}, db = openDatabase('learning_js',
'1.0',
'Database to create on tinkerbin',
2 * 1024 * 1024);
ns.createDB = function(){
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS 'primary' (id unique, text)");
});
};
ns.insertValues = function(id, text){
db.transaction(function(tx){
tx.executeSql("INSERT INTO 'primary' (id, text) VALUES (?, ?)", [id, text]);
});
};
ns.readValues = function(el){
el.empty();
db.transaction(function(tx){
tx.executeSql("SELECT * FROM 'primary'", [], function (tx, results) {
var len = results.rows.length, i;
for (i = 0; i < len; i++) {
el.append(results.rows.item(i).id + '&nbsp;&nbsp;&nbsp;' + results.rows.item(i).text + '\n\r');
}
});
});
};
$(function(){
$('div.button').bind({
'click.newDB' : function(){
ns.createDB();
ns.insertValues(Math.random(), 'Test' + Math.random());
ns.readValues($('#source'));
}
});
});
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment