Skip to content

Instantly share code, notes, and snippets.

@Groxx
Created November 1, 2010 03:16
Show Gist options
  • Save Groxx/657546 to your computer and use it in GitHub Desktop.
Save Groxx/657546 to your computer and use it in GitHub Desktop.
Core functionality of HTML5 WebDatabases
var db = openDatabase("short_name" // actual database name. Opens existing or creates.
, "1" // version number. *Must* be correct.
, "a long descriptiony thing" // I do not know where this is useful. Nothing seems to see it...
, 10 * 1024*1024); // for 10 megabytes
// also: db.readTransaction(); identical to below, but read-only access to the database.
db.transaction(function(tx){
tx.executeSql("select * from your_table where aColumn = ?"
, ["aParameter"] // ? + [data] guards against injection, and is the *only* safe way to do so. One data item per ?, in order.
, function(tx, results) { // success callback
// results contains the num of rows affected, similar to other SQL engines
console.log("Rows affected: " + results.rowsAffected);
for(var i = 0; i < results.rows.length; i++){
// Do things with the row item here
var item = results.rows.item(i);
// item = {column:value, column:value}
// If you inserted:
var last_inserted_rowid = results.insertId
// otherwise this throws an exception!!!
}
}
, function(tx, error) { // error callback
console.error("code: " + error.code + "\n" + "message: " + error.message);
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment