时间:2021-07-01 10:21:17 帮助过:22人阅读
var db = openDatabase(‘mydb‘, ‘1.0‘, ‘Test DB‘, 2 * 1024 * 1024);
执行操作使用 database.transaction() 函数:
var db = openDatabase(‘mydb‘, ‘1.0‘, ‘Test DB‘, 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql(‘CREATE TABLE IF NOT EXISTS LOGS (id unique, log)‘);
});
这样就会在mydb中插入一个 LOGS的表
在执行上面的创建表语句后,我们可以插入一些数据:
var db = openDatabase(‘mydb‘, ‘1.0‘, ‘Test DB‘, 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql(‘CREATE TABLE IF NOT EXISTS LOGS (id unique, log)‘);
tx.executeSql(‘INSERT INTO LOGS (id, log) VALUES (1, "啦啦啦")‘);
tx.executeSql(‘INSERT INTO LOGS (id, log) VALUES (2, "www.baidu.com")‘);
});
如何读取数据库中已经存在的数据:
var db = openDatabase(‘mydb‘, ‘1.0‘, ‘Test DB‘, 2 * 1024 * 1024); db.transaction(function (tx) { tx.executeSql(‘CREATE TABLE IF NOT EXISTS LOGS (id unique, log)‘); tx.executeSql(‘INSERT INTO LOGS (id, log) VALUES (1, "菜鸟教程")‘); tx.executeSql(‘INSERT INTO LOGS (id, log) VALUES (2, "www.runoob.com")‘); }); 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++){ alert(results.rows.item(i).log ); } }, null); });
HTML5 Web SQL 数据库操作
标签:alert base pre 大小 方法 query rom com 核心