Created
September 26, 2011 23:03
-
-
Save buzzedword/1243690 to your computer and use it in GitHub Desktop.
Messing around with Web SQL API
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
(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 + ' ' + 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