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) { //执行成功的回调函数
//在这里对result 做你想要做的事情吧...........
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('查询失败: ' + 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);
});
});
}

转载于:https://www.cnblogs.com/-maomao/p/4935836.html

猜你喜欢

转载自blog.csdn.net/weixin_34342578/article/details/93760825