phonegap中操作数据库的示例分析

本文讲解"phonegap中操作数据库的示例分析",用于解决相关问题。

这次给大家带来phonegap操作数据库详解,phonegap操作数据库的注意事项有哪些,下面就是实战案例,一起来看一下。

实例如下:

<!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> 
        <h2>Names</h2> 
        <p id='status'></p> 
    </body> 
  
</html>

关于 "phonegap中操作数据库的示例分析" 就介绍到此。希望多多支持编程宝库

phonegap怎样位置信息:本文讲解"phonegap如何位置信息",用于解决相关问题。实例如下:<!DOCTYPE html> <html> <head> <meta charset="UT ...