HTML5 WEB SQL

var database = openDatabase("student1", "1.0", "学生表", 1024 * 1024, function () { })
this.createTable = function () {
database.transaction(function (tx) {
tx.executeSql(
"create table if not exists stu (id REAL UNIQUE, name TEXT)",
[],
function (tx, result) { alert('创建stu表成功'); },
function (tx, error) {
alert('创建stu表失败:' + error.message);
});
});
}
this.insert = function (a,b) {
database.transaction(function (tx) {
tx.executeSql(
"insert into stu (id, name) values(?, ?)",
[a, b],
function () { alert('添加数据成功'); },
function (tx, error) {
alert('添加数据失败: ' + error.message);
});
});
};

This.query = function () {
database.transaction (function (TX) {
tx.executeSql (
"SELECT * from STU", [],
function (TX, Result) {// perform a successful callback
// result here to do what you want to do it ...........
for (var i = 0; i <result.rows.length; i ++) {
console.log (result.rows .Item (I) [ 'ID'] + "-" + result.rows.item (I) [ "name"])
}

},
function (TX, error) {
Alert ( 'query failed:' + error. Message);
});
});
}

this.update = function (id, name) {
database.transaction(function (tx) {
tx.executeSql(
"update stu set name = ? where id= ?",
[name, id],
function (tx, result) {
},
function (tx, error) {
alert('更新失败: ' + error.message);
});
});
}

this.del = function (id) {
dataBase.transaction(function (tx) {
tx.executeSql(
"delete from stu where id= ?",
[id],
function (tx, result) {
},
function (tx, error) {
alert('删除失败: ' + error.message);
});
});
}

Reproduced in: https: //www.cnblogs.com/-maomao/p/4935836.html

Guess you like

Origin blog.csdn.net/weixin_34342578/article/details/93760825