HTML5学习笔记 本地数据库

 Google已经在Chrome 中通过SQLite提供对Web SQL Database的支持

接口说明:

1.打开一个数据库:

  var db = openDatabase('mydb', '1.0', 'test', 2 * 1024 * 1024, function(db){ });

      参数说明:数据库名(mydb) | 版本号(1.0) | 数据库描述( test ) | 数据库大小(2*1024*1024) | 创建回调

2.数据库创建好后可以执行SQL的查询和更新操作了

  执行SQL前要先拿到一个事务连接

  db.transaction(function (tx) {

    //在连接的回调函数中执行SQL语句  

     tx.executeSql('CREATE TABLE IF NOT EXISTS SETS (key unique, value)');    

  });

  tx.executeSql函数说明:

 参素列表:SQL语句 | 值([]) | 成功回调函数 | 失败回调函数

例子:

db.transaction(function (tx) {

  //创建

    tx.executeSql('CREATE TABLE IF NOT EXISTS SETS (key unique, value)');  

  //插入

   tx.executeSql('INSERT INTO SETS ( key, value) VALUES (?, ?)' , [1,'asdf'],function(tx,result){},function(tx,error){}); 

  //查询

   tx.executeSql('SELECT * from  SETS ' , [],function(tx,result){

  var len = result.rows.length;

  var item = result.rows.item(len - 1);

  var key = item['key'];

  },function(tx,error){}); 

  //修改

    tx.executeSql(' UPDATE SETS SET value = ? WHERE key = ? ',['hahaha',1],function(tx,result){},function(tx,error){ });

  //删除

   tx.executeSql('DELETE from SETS',[],function(tx,result){},function(tx,error){ });

  });

原文地址:https://www.cnblogs.com/feng_013/p/1975212.html