0

I am developing ionic cordova hybrid application. When I test it using browser, application works very well. But I test it in my real device it not works very persistent. It means that SQLite sometimes works well and sometimes do not works well. The following is my code:

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 Answer 1

1

Try the following:

1. Load your ng-cordova-min.js or ng-cordova.js and cordova.js files last by placing them at the bottom of your in your index.html file

2. In your app.js file, database initialisation should be the first line i.e.

    var db = null;

should be the first line at the top. And still in your app.js file,

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

should be the first line in the platform ready function. Your code should like something like this in the app.js file;

.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();
        }
    });
})
Sign up to request clarification or add additional context in comments.

1 Comment

not clear why movie the files to the end of index will fix the issue? anyway I have the same problem, the execution sometimes work and sometimes not without any error or exception. it's a strange case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.