Phonegap Storage&一旦您在iOS平台中杀死应用程序,数据库数据就会丢失

时间:2016-04-16 18:48:29

标签: ios sqlite cordova phonegap-plugins

我正在尝试使用Cordova Storage API在我的Phonegap应用中保留一些数据。 但每当我在杀死它后打开应用程序时,它会在执行SELECT查询后返回2 rows found

我正在使用:

  • Cordova:5.2.0
  • Cordova iOS平台3.9.2
  • XCode:7.0.1
  • iOS模拟器9.0

代码中是否有任何错误:

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

    // Wait for Cordova 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) {
        var len = results.rows.length;
        alert("DEMO table: " + len + " rows found.");

    }

    // Transaction error callback
    //
    function errorCB(err) {
        alert("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)

如果您不想丢失数据,请删除以下行:

tx.executeSql('DROP TABLE IF EXISTS DEMO');

因为这会在您初始化应用时删除所有数据。