PhoneGap 的存储 API_Web Sql

一、介绍

  此 API 基于 W3C WEB SQL Database Specification 和 W3C Web Storage API Specification。

  有些 设备已经提供了对该规范的实现,对于这些设备采用内置实现而非使用 PhoneGap 的实现。

  对于没有存储支持的设 备,PhoneGap 的实现应该是完全兼容 W3C 规范。

二、openDatabase方法

  window.openDatabase(name, version, display_name, size)

  该方法将创建一个新的 SQL Lite 数据库,并返回该 Database 对象。可使用该 Database 对象 操作数据。

  name :数据库的名称。

  version:数据库的版本号。

  display_name:数据库的显示名。

  size:以字节为单位的数据库大小。

<!DOCTYPE html> 
<html>
<head>
<meta charset="utf-8">
<title>jQuery  Mobile  Web 应用程序</title>
<link href="../jquery.mobile-1.3.2.css" rel="stylesheet" type="text/css"/>
<script src="../jquery.js" type="text/javascript"></script>

<script src="../jquery.mobile-1.3.2.js" type="text/javascript"></script>
<script src="../cordova.js" type="text/javascript"></script>
<script type="text/javascript">

    //1.如果数据库存在 则打开  不存在的则创建 然后在打开
    var db = window.openDatabase("phonegap100", "1.0","phonegap中文网",1024*1024*20);
    if(db){
        console.log('数据库连接成功');
    }else{
        console.log('数据库连接失败');
    }
    
    //2.执行transaction操作数据库  
    db.transaction(function(tx){
        tx.executeSql("CREATE TABLE test (id int UNIQUE, name TEXT, age int)");    //创建表
    })

    //插入数据
    db.transaction(function(tx){
        //四个参数  第一个 sql语句  第二个 sql参数  第三个 成功的回调函数 第四个 失败的回调函数
        //tx.executeSql("insert into test(id,name,age) values(1,'张三',20)");        
        //tx.executeSql("insert into test(id,name,age) values(2,'李四',25)");
        
        //tx.executeSql("insert into test(id,name,age) values(?,?,?)",[3,'赵四',15],null,null);
        
        tx.executeSql("insert into test(id,name,age) values(3,'赵五',25)",[],function(tx,rel){
            if(rel.rowsAffected>0){    
                //新增成功
                console.log('成功的增加一条数据');
                console.log('这条数据的id'+rel.insertId);
            }        
        },function(){
            alert('失败');
        });
        
    });
    
    //更新数据
    db.transaction(function(tx){
        //tx.executeSql    
        //四个参数  第一个 sql语句  第二个 sql参数  第三个 成功的回调函数 第四个 失败的回调函数
        //tx.executeSql("insert into test(id,name,age) values(1,'张三',20)");        
        //tx.executeSql("insert into test(id,name,age) values(2,'李四',25)");
        
        //tx.executeSql("update test set name=?,age=? where id=1",['张三1',50],null,null);
        
        //tx.executeSql("update test set name='张三22',age=60 where id=1");
    })

    //删除数据
    db.transaction(function(tx){
        //tx.executeSql    
        //四个参数  第一个 sql语句  第二个 sql参数  第三个 成功的回调函数 第四个 失败的回调函数
        
        
        //tx.executeSql("update test set name=?,age=? where id=1",['张三1',50],null,null);
        
        //tx.executeSql("delete from test where id=1");
    })

    //查询数据
    db.transaction(function(tx){
        //tx.executeSql    
        //四个参数  第一个 sql语句  第二个 sql参数  第三个 成功的回调函数 第四个 失败的回调函数
        
        
                
        tx.executeSql("select * from test",[],function(tx,rel){
            for(var i=0;i<rel.rows.length;i++){
                document.write('姓名:'+rel.rows.item(i)['name']+',年龄:'+rel.rows.item(i)['age']+'<br>');
            }
            
        },function(){
            alert('失败');
        });
    })
</script>
</head> 
<body>
</body>
</html>
原文地址:https://www.cnblogs.com/LO-ME/p/4586829.html