使用phonegap操作數(shù)據(jù)庫的實現(xiàn)方法
2017/8/19 9:27:07來源:互聯(lián)網(wǎng)
實例如下:
<!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); } //初始化數(shù)據(jù)庫數(shù)據(jù) 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); } //查詢成功回調(diào) 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; } //執(zhí)行初始化數(shù)據(jù)成功后的回調(diào) function successDB(){ var db=window.openDatabase("Test","1.0","Test",200000); db.transaction(queryDB,errorDB); } //失敗回調(diào) function errorDB(err){ alert('Error processing SQL:'+err.code); } </script> </head> <body> <h1>Names</h1> <div id='status'></div> </body> </html>
以上這篇使用phonegap操作數(shù)據(jù)庫的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持圖趣網(wǎng)。
[教程作者:佚名]
免責聲明:本站文章系圖趣網(wǎng)整理發(fā)布,如需轉載,請注明出處,素材資料僅供個人學習與參考,請勿用于商業(yè)用途!
本文地址:http://pkvc.cn/tutorial/wd356.html
本文地址:http://pkvc.cn/tutorial/wd356.html
這些是最新的
最熱門的教程