教程 > html5 教程 > 阅读:36

html5 web sql——迹忆客-ag捕鱼王app官网

web sql 数据库 api 并不是 html5 规范的一部分,但是它是一个独立的规范,引入了一组使用 sql 操作客户端数据库的 apis。

如果你是一个 web 后端程序员,应该很容易理解 sql 的操作。

你也可以参考我们的 sql 教程,了解更多数据库操作知识。

web sql 数据库可以在最新版的 safari, chrome 和 opera 浏览器中工作。


核心方法

以下是规范中定义的三个核心方法:

  • opendatabase:这个方法使用现有的数据库或者新建的数据库创建一个数据库对象。
  • transaction:这个方法让我们能够控制一个事务,以及基于这种情况执行提交或者回滚。
  • executesql:这个方法用于执行实际的 sql 查询。

打开数据库

我们可以使用 opendatabase() 方法来打开已存在的数据库,如果数据库不存在,则会创建一个新的数据库,使用代码如下:

var db = opendatabase('mydb', '1.0', 'test db', 2 * 1024 * 1024);

opendatabase() 方法对应的五个参数说明:

  • 数据库名称
  • 版本号
  • 描述文本
  • 数据库大小
  • 创建回调

第五个参数,创建回调会在创建数据库后被调用。


执行查询操作

执行操作使用 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.jiyik.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 (?, ?)', [e_id, e_log]);
});

实例中的 e_id 和 e_log 是外部变量,executesql 会映射数组参数中的每个条目给 "?"。


读取数据

以下实例演示了如何读取数据库中已经存在的数据:

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 = "

查询记录条数: " len "

"; document.queryselector('#status').innerhtml = msg; for (i = 0; i < len; i ){ alert(results.rows.item(i).log ); } }, null); });

完整实例

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, "迹忆客")');
    tx.executesql('insert into logs (id, log) values (2, "www.jiyik.com")');
    msg = '

数据表已创建,且插入了两条数据。

'; document.queryselector('#status').innerhtml = msg; }); db.transaction(function (tx) { tx.executesql('select * from logs', [], function (tx, results) { var len = results.rows.length, i; msg = "

查询记录条数: " len "

"; document.queryselector('#status').innerhtml = msg; for (i = 0; i < len; i ){ msg = "

" results.rows.item(i).log "

"; document.queryselector('#status').innerhtml = msg; } }, null); });

删除记录

删除记录使用的格式如下:

db.transaction(function (tx) {
    tx.executesql('delete from logs  where id=1');
});

删除指定的数据id也可以是动态的:

db.transaction(function(tx) {
    tx.executesql('delete from logs where id=?', [id]);
});

更新记录

更新记录使用的格式如下:

db.transaction(function (tx) {
    tx.executesql('update logs set log=\'www.jiyik.com\' where id=2');
});

更新指定的数据id也可以是动态的:

db.transaction(function(tx) {
    tx.executesql('update logs set log=\'www.w3cschool.cc\' where id=?', [id]);
});

完整实例

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, "迹忆客")');
    tx.executesql('insert into logs (id, log) values (2, "www.jiyik.com")');
    msg = '

数据表已创建,且插入了两条数据。

'; document.queryselector('#status').innerhtml = msg; }); db.transaction(function (tx) { tx.executesql('delete from logs where id=1'); msg = '

删除 id 为 1 的记录。

'; document.queryselector('#status').innerhtml = msg; }); db.transaction(function (tx) { tx.executesql('update logs set log=\'www.w3cschool.cc\' where id=2'); msg = '

更新 id 为 2 的记录。

'; document.queryselector('#status').innerhtml = msg; }); db.transaction(function (tx) { tx.executesql('select * from logs', [], function (tx, results) { var len = results.rows.length, i; msg = "

查询记录条数: " len "

"; document.queryselector('#status').innerhtml = msg; for (i = 0; i < len; i ){ msg = "

" results.rows.item(i).log "

"; document.queryselector('#status').innerhtml = msg; } }, null); });

查看笔记

扫码一下
查看教程更方便
网站地图