簡體   English   中英

Ionic Cordova SQLite在瀏覽器上運行良好,但在Android設備上無法持久運行

[英]Ionic Cordova SQLite works well on browser but does not work persistent in Android device

我正在開發離子科爾多瓦混合應用程序。 當我使用瀏覽器進行測試時,應用程序運行良好。 但是我在我的真實設備中對其進行了測試,但它並不是非常持久。 這意味着SQLite有時效果很好,有時效果不好。 以下是我的代碼:

app.js

.run(function($ionicPlatform, $cordovaSQLite) {
  $ionicPlatform.ready(function() {
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      StatusBar.styleDefault();
    }

    db = window.openDatabase("chatChannel.db", "1", "Demo SQLite Test", "2000");
    $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS chat_channel(id interger primary key, chat_room text, last_text text, username text, chat_channel text unique)");
    $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS chat_content(id integer primary key, content text, channel text, chat_flag integer, username text, date timestamp)");
  });

controller.js

   var query = "SELECT * FROM chat_content WHERE channel=? ORDER BY date";
    var promise =  $cordovaSQLite.execute(db, query, [subscribeChannel]).then(function(result){
    for(i=0; i<result.rows.length; i++){
        $scope.messages.push(result.rows.item(i));
        console.log(result.rows.item(i));
        }
    });  

請嘗試以下操作:

1.通過將它們放置在index.html文件的底部,最后加載ng-cordova-min.jsng-cordova.jscordova.js文件

2.在您的app.js文件中,數據庫初始化應為第一行,即

    var db = null;

應該是頂部的第一行。 仍然在您的app.js文件中,

    db = window.openDatabase("chatChannel.db", "1", "Demo SQLite Test", "2000");

應該是平台就緒功能的第一行。 您的代碼應在app.js文件中類似這樣;

.run(function($ionicPlatform, $cordovaSQLite) {
        $ionicPlatform.ready(function() {
        db = window.openDatabase("chatChannel.db", "1", "Demo SQLite Test", "2000");

        $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS chat_channel(id interger primary key, chat_room text, last_text text, username text, chat_channel text unique)");
        $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS chat_content(id integer primary key, content text, channel text, chat_flag integer, username text, date timestamp)");

        if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }
    });
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM