2014-03-13 148 views
0

我是Phonegap/cordova的初學者,所以請耐心等待。Cordova - openDatabase連接無法正常工作

我有一個空白的應用程序,我想連接到數據庫並顯示結果。以下是我的代碼..

<!DOCTYPE html> 
<html> 
<head> 
<title>Page Title</title> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="jquery.mobile-1.4.2.css" /> 
<script type="text/javascript" src="js/jquery.js"></script> 
<script type="text/javascript" src="js/cordova.js"></script> 
<script type="text/javascript" src="js/index.js"></script> 
<script type="text/javascript" src="js/jquery.mobile-1.4.2.js"></script> 
<script type="text/javascript"> 
//add listener when device ready 
    document.addEventListener("deviceready", onDeviceReady, false); 
    var db = window.openDatabase("Dummy_DB", "1.0", "Just a Dummy DB", 0); //will create database Dummy_DB or open it 

    //function will be called when device ready 
    function onDeviceReady(){ 
     db.transaction(populateDB, errorCB, successCB); 
    } 

    //create table and insert some record 
    function populateDB(tx) { 
     tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerPlayer (id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL, Club TEXT NOT NULL)'); 
     tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Alexandre Pato", "AC Milan")'); 
     tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Van Persie", "Arsenal")'); 
    } 

    //function will be called when an error occurred 
    function errorCB(err) { 
     alert("Error processing SQL: "+err.code); 
    } 

    //function will be called when process succeed 
    function successCB() { 
     alert("success!"); 
     db.transaction(queryDB,errorCB); 
    } 

    //select all from SoccerPlayer 
    function queryDB(tx){ 
     tx.executeSql('SELECT * FROM SoccerPlayer',[],querySuccess,errorCB); 
    } 

    function querySuccess(tx,result){ 
     $('#SoccerPlayerList').empty(); 
     $.each(result.rows,function(index){ 
      var row = result.rows.item(index); 
      $('#SoccerPlayerList').append('<li><a href="#"><h3 class="ui-li-heading">'+row['Name']+'</h3><p class="ui-li-desc">Club '+row['Club']+'</p></a></li>'); 
     }); 

     $('#SoccerPlayerList').listview(); 
    } 
</script> 
</head> 
<body> 

<div data-role="page"> 
    <div data-role="header" data-position="fixed" data-theme="b"> 
    <h1>Soccer Player</h1> 
    </div> 
    <div data-role="content"> 
    <ul id="SoccerPlayerList"> 
    </ul> 
    </div> 
</div> 
<!--end of Soccer Player Page---> 
</body> 
</html> 

該代碼似乎根本沒有創建或讀取任何數據庫。當我將應用程序加載到我的android手機上時,即使在瀏覽器中加載它時也不會顯示任何結果。

我沒有收到任何控制檯錯誤,但在我的LogCat error opening trace file: No such file or directory (2)中出現錯誤。

我錯過了什麼使數據庫連接的工作?或者有什麼我在代碼中看不到的東西是錯的?

回答

1

更改此:

//add listener when device ready 
document.addEventListener("deviceready", onDeviceReady, false); 
var db = window.openDatabase("Dummy_DB", "1.0", "Just a Dummy DB", 0); //will create database Dummy_DB or open it 

//function will be called when device ready 
function onDeviceReady(){ 
    db.transaction(populateDB, errorCB, successCB); 
} 

TO:

//add listener when device ready 
document.addEventListener("deviceready", onDeviceReady, false); 
var db; 

//function will be called when device ready 
function onDeviceReady(){ 
    db = window.openDatabase("Dummy_DB", "1.0", "Just a Dummy DB", 0); //will create database Dummy_DB or open it 
    db.transaction(populateDB, errorCB, successCB); 
} 

你需要等待deviceReady調用定義你的數據庫。