<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="showInfo"></div>
<script>
var db=openDatabase("myDB","1.0","test",1024*1024);
//添加数据
var msg='';
db.transaction(function(tx){
//tx是一个对象.该对象有个executeSql这个方法
tx.executeSql("CREATE TABLE IF NOT EXISTS STUDENT (id UNIQUE,name)");
tx.executeSql("INSERT INTO STUDENT (id,name) VALUES (1,'张三')");
tx.executeSql("INSERT INTO STUDENT (id,name) VALUES (2,'网上')");
tx.executeSql("INSERT INTO STUDENT (id,name) VALUES (3,'李四')");
tx.executeSql("INSERT INTO STUDENT (id,name) VALUES (4,'王麻子')");
tx.executeSql("INSERT INTO STUDENT (id,name) VALUES (5,'杜三')");
});
//删除数据
db.transaction(function(tx){
tx.executeSql("DELETE FROM STUDENT WHERE id=3")
});
db.transaction(function(tx){
tx.executeSql("SELECT * FROM STUDENT", [] ,function(tx,res){
var len=res.rows.length;//查询数据
msg="<span>查询的数据有"+len+"条</span>>";
for(var i=0;i<len;i++){
msg+="<p>"+res.rows.item(i).name+"</p>"
}
document.getElementById("showInfo").innerHTML=msg;
})
});
//更新数据
db.transaction(function(tx){
tx.executeSql("UPDATE STUDENT SET name='小花' WHERE id=4")
})
</script>
</body>
</html>