在phoneGap中创建和使用数据库不起作用

时间:2014-07-09 12:40:57

标签: android cordova

我是PhoneGap(2.9.0)的新用户,我正在尝试创建一个数据库并运行Android模拟器,但它没有正常,而是给出了一些错误,例如“不幸的是应用程序已被停止”,我不知道我的下面代码出了什么问题。

请不要将我的问题视为重复,因为我已经看到许多关于流量的问题,并尝试解决问题,但我没有得到任何解决方案。

我认为即使我包含cordova.js文件,'deviceready'事件也无法正常工作。

以下是我的代码段:

<!DOCTYPE html>
<html>
  <head>
    <title>Contact Example</title>

    <script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // Populate the database 
    //
    function populateDB(tx) {
        tx.executeSql('DROP TABLE IF EXISTS DEMO');
        tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
    }

    // Query the database
    //
    function queryDB(tx) {
        tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
    }

    // Query the success callback
    //
    function querySuccess(tx, results) {
        // this will be empty since no rows were inserted.
        console.log("Insert ID = " + results.insertId);
        // this will be 0 since it is a select statement
        console.log("Rows Affected = " + results.rowAffected);
        // the number of rows returned by the select statement
        console.log("Insert ID = " + results.rows.length);
    }

    // Transaction error callback
    //
    function errorCB(err) {
        console.log("Error processing SQL: "+err.code);
    }

    // Transaction success callback
    //
    function successCB() {
        var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
        db.transaction(queryDB, errorCB);
    }

    // PhoneGap is ready
    //
    function onDeviceReady() {
        var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
        db.transaction(populateDB, errorCB, successCB);
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Database</p>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

我通过小修正获得了查询的输出。

1)我已经为cordova.js文件提供了正确的文件 2)从&#39; successCB&#39;中删除开放数据库。功能并使其全球化。

以下代码已成功执行。

<!DOCTYPE html>
<html>
  <head>
    <title>Storage Example</title>
    <script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

      var db;
    // Populate the database
    //
    function populateDB(tx) {
        alert('Table DEMO will be created');
        tx.executeSql('DROP TABLE IF EXISTS DEMO');
        tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
    }

    // Query the database
    //
    function queryDB(tx) {
        tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
    }

    // Query the success callback
    //
    function querySuccess(tx, results) {
        var htmlString = '';
        var len = results.rows.length;
        alert("No of rows in the table " + len);
        // this will be true since it was a select statement and so rowsAffected was 0
        if (!results.rowsAffected) {
            console.log('No rows affected!');
            return false;
        }

        for(var i=0; i<len; i++) {
        htmlString += '<li>' + results.rows.item(i).id + '<br/>' + results.rows.item(i).data + '</li>';
        }
        $('#listview').html(htmlString);
        // for an insert statement, this property will return the ID of the last inserted row
        console.log("Last inserted row ID = " + results.insertId);
    }

    // Transaction error callback
    //
    function errorCB(err) {
        console.log("Error processing SQL: "+err.code);
    }

    // Transaction success callback
    //
    function successCB() {
        alert('successed');
        db.transaction(queryDB, errorCB);
    }

    // device APIs are available
    //
    function onDeviceReady() {
        alert("Database has been started");
        db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
        alert(db);
        db.transaction(populateDB, errorCB, successCB);
        return false;
    }
    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Database</p>
    <ul id="listview"></ul>
  </body>
</html>