圆月山庄资源网 Design By www.vgjia.com

实例如下:

<!DOCTYPE html> 
<html> 
  
    <head> 
        <meta charset="UTF-8"> 
        <title>Database Example</title> 
  
        <script type="text/javascript" charset="UTF-8" src="cordova.js"></script> 
        <script type="text/javascript" charset="UTF-8"> 
            document.addEventListener("deviceready", onDeviceReady, false); 
  
            function onDeviceReady() { 
                var db=window.openDatabase("Test","1.0","Test",200000); 
                db.transaction(populateDB,errorDB,successDB); 
            } 
              
            //初始化数据库数据 
            function populateDB(tx){ 
                tx.executeSql('DROP TABLE IF EXISTS test1'); 
                tx.executeSql('CREATE TABLE IF NOT EXISTS test1 (id unique,name)'); 
                tx.executeSql('INSERT INTO test1(id,name) VALUES(1,"Tony")'); 
                tx.executeSql('INSERT INTO test1(id,name) VALUES(2,"Bill")'); 
                tx.executeSql('INSERT INTO test1(id,name) VALUES(3,"Tony")'); 
            } 
              
            //查询方法 
            function queryDB(tx){ 
                tx.executeSql('SELECT * FROM test1',[],querySuccess,errorDB); 
            } 
              
            //查询成功回调 
            function querySuccess(tx,results){ 
                var len=results.rows.length; 
                var status=document.getElementById("status"); 
                var string ="Rows:"+len+"<br />"; 
                for(var i=0;i<len;i++){ 
                    string+=results.rows.item(i).name+"<br />"; 
                } 
                status.innerHTML=string; 
            } 
              
            //执行初始化数据成功后的回调 
            function successDB(){ 
                var db=window.openDatabase("Test","1.0","Test",200000); 
                db.transaction(queryDB,errorDB); 
            } 
              
            //失败回调 
            function errorDB(err){ 
                alert('Error processing SQL:'+err.code); 
            } 
              
        </script> 
    </head> 
  
    <body> 
        <h1>Names</h1> 
        <div id='status'></div> 
    </body> 
  
</html>

以上这篇使用phonegap操作数据库的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
phonegap,操作,数据库

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com