Prototype
openDatabase(name, version, displayName, estimatedSize, creationCallback)
// to open database
var db;
openDatabase('documents', '1', 'Local document storage', 1024 * 1024,
function(database) {
db = database;
});
}
// another example to open database
db = openDatabase('documents', '1', 'Local document storage', 1024 * 1024);
Prototype
transaction(callback, errorCallback, successCallback)
// to create transaction
db.transaction(
function(tx) {
...
},
function(error) {
...
},
function() {
...
}
}
Prototype
executeSql(sql, args, callback, errorCallback)
// to execute sql for non-query command
tx.executeSql("create table Book(id, name)");
tx.executeSql("insert into Book values(?, ?)", [1, "good book"]);
var name = "bad book";
tx.executeSql("update Book set name = ?", [name]);
// to execute sql for query command
tx.executeSql("select id, name from Book", [],
function(tx, rs) {
for (var i=0; i < rs.rows.length; i++) {
var row = rs.rows.item(i);
var bookData = "ID=" + row.id + " / Name=" row.name;
}
});
Example
var db = openDatabase('Book', '1', 'Local storage', 1024 * 1024);
db.transaction(
function(tx) {
tx.executeSql("drop table Book");
});
db.transaction(
function(tx) {
tx.executeSql("create table Book(id integer primary key autoincrement, name)");
});
db.transaction(
function(tx) {
tx.executeSql("insert into Book (name) values(?)", ["book1"],
function(tx, rs) {
alert("id=" + rs.insertId);
});
});
db.transaction(
function(tx) {
tx.executeSql("select id, name from Book", [],
function(tt, rs) {
for (var i = 0; i < rs.rows.length; i++) {
var row = rs.rows.item(i);
alert(row.name);
}
});
});
db.transaction(
function(tx) {
tx.executeSql("drop table Book");
});
db.transaction(
function(tx) {
tx.executeSql("create table Book(id integer primary key autoincrement, name)");
});
db.transaction(
function(tx) {
tx.executeSql("insert into Book (name) values(?)", ["book1"],
function(tx, rs) {
alert("id=" + rs.insertId);
});
});
db.transaction(
function(tx) {
tx.executeSql("select id, name from Book", [],
function(tt, rs) {
for (var i = 0; i < rs.rows.length; i++) {
var row = rs.rows.item(i);
alert(row.name);
}
});
});
Reference:
HTML5 & API NYUMON by Shunpei Shiraishi
http://dev.w3.org/html5/webdatabase/#introduction
沒有留言:
張貼留言