<!DOCTYPE HTML>
<html>
<head>
<title>数据库查询</title>
<script type="text/javascript">
function my(){
var db = openDatabase('mydb1', '1.0', 'Test DB', 2 * 1024 * 1024);
var msg;
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, 1号电机)');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "1233")');
msg = '<body><p>数据表已创建,且插入了两条数据。</p>';
document.querySelector('#status').innerHTML = msg;
});
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
var len = results.rows.length, i;
msg = "<p>查询记录条数: " + len + "</p>";
document.querySelector('#status').innerHTML += msg;
for (i = 0; i < len; i++){
msg = "<p><b>" + results.rows.item(i).log + "</b></p>";
document.querySelector('#status').innerHTML += msg;
}
}, null);
});}
function my1(){
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
var msg;
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, 2号电机)');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "1233")');
msg = '<body><p>数据表已创建,且插入了两条数据。</p>';
document.querySelector('#status').innerHTML = msg;
});
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
var len = results.rows.length, i;
msg = "<p>查询记录条数: " + len + "</p>";
document.querySelector('#status').innerHTML += msg;
for (i = 0; i < len; i++){
msg = "<p><b>" + results.rows.item(i).log + "</b></p>";
document.querySelector('#status').innerHTML += msg;
}
}, null);
});}
</script>
</head>
<body>
<button οnclick="my()">1号电机</button><!--分别对应不同函数-->
<button οnclick="my1()">2号电机</button>
</body>
<body>
<div id="status" name="status">Status Message</div>
</body>
</html>
websql
最新推荐文章于 2024-05-07 12:27:36 发布