Cordova - openDatabase连接无法正常工作

时间:2014-03-13 15:09:02

标签: android database cordova

我是Phonegap / cordova的初学者,所以请耐心等待。

我有一个空白的应用程序,我想连接到数据库并显示结果。以下是我的代码..

<!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 个答案:

答案 0 :(得分: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调用以定义您的数据库。