PHP前端开发

phonegap使用方法介绍(八)操作数据库

百变鹏仔 2个月前 (10-18) #H5教程
文章标签 使用方法

下面小编就为大家带来一篇使用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(&#39;DROP TABLE IF EXISTS test1&#39;);                 tx.executeSql(&#39;CREATE TABLE IF NOT EXISTS test1 (id unique,name)&#39;);                 tx.executeSql(&#39;INSERT INTO test1(id,name) VALUES(1,"Tony")&#39;);                 tx.executeSql(&#39;INSERT INTO test1(id,name) VALUES(2,"Bill")&#39;);                 tx.executeSql(&#39;INSERT INTO test1(id,name) VALUES(3,"Tony")&#39;);             }                           //查询方法             function queryDB(tx){                 tx.executeSql(&#39;SELECT * FROM test1&#39;,[],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(&#39;Error processing SQL:&#39;+err.code);             }                       </script>     </head>       <body>         <h1>Names</h1>         <p id=&#39;status&#39;></p>     </body>   </html>