SQlite only works on intalation, if I close the app stops working


#1

I have an app with a SQLite DB, when I install the app
and I click a link that goes to a search , I can access to
search the SQLite data loaded, everything works well. If
I add a word to the search form and I can see the list off the
that word that I put in the search form field.
But when I close the app and open it back again, and click on the
search link I can´t access neither to the data that
is loaded.

I wonder if the problem is that the second time I open the app
when I click the link to search I can´t find the DB or I need
another code to work along with the link, to find the form an
load the data from the DB ?

I begin loading the data in www/js/app.js to the DB located in
www/contract.db

function bdSQLite() {
window.plugins.sqlDB.copy("contract.db", function() {
db = $cordovaSQLite.openDB("contract.db");
}, function(error) {
console.error("Error al cargar bd " + error);
db = $cordovaSQLite.openDB("contract.db");
});
};

The view is in \www\templates\application-storage\sqLite\html
\contract-list.html

<md-list-item>
            <md-input-container md-no-float="" class="md-list-item-full-width">
                <input ng-model="filterText" placeholder="Buscar contactos." type="text"       autocomplete="off" name="filtername"  ng-model-options="{ debounce: 1000 }" >
				 
            </md-input-container>
        </md-list-item>

        <!--Below code it will disable animation to better performance-->
        <div ng-if="filterText.length>2" class="row contract-list-item"
             ng-repeat="contract in contracts  | filter: filterText" >
            <div class="col-25 icon-user">
                <i class="fa fa-user"></i>
            </div>
            <div class="col-50">
                {{ contract.contacto }} {{ contract.categoria }}
            </div>

The controller is in \www\templates\application-storage\sqLite
\js\controller.js also the services in services.js
In the controller.js I make the call to obtein the data
$scope.getContractList();

$scope.getContractList = function () {
    $scope.contracts = ContractDB.all();
};//End getContractList.

and access to the services

all: function ()
    {
        contractList = [];

        // Variable for prepare query statement to select all contracts.
        var query = "SELECT * FROM contracts";

        // Execute query statement from query variable.
        $cordovaSQLite.execute(db, query).then(function (res)

        {
            if (res.rows.length > 0)
            {
                for (var i = 0; i < res.rows.length; i++)
                {
                    var dataItem = {
                        id          : res.rows.item(i).id           ,
                        contacto   : res.rows.item(i).contacto    ,
                        categoria    : res.rows.item(i).categoria     ,
                        telephone   : res.rows.item(i).telephone    ,
                        email       : res.rows.item(i).email        
                    };
                    contractList.push(dataItem);
                }
            }
        });
        return contractList;
    },// End select all data.
....

I use Versions
Ionic 1.7.14
Cordova 5.3.3
Node.js 4.4.

and the following plugins
Plugins
“cordovaPlugins”:
“ionic-plugin-keyboard@1.0.8”,
“cordova-plugin-googleplayservices@19.0.3”,
“cordova-plugin-console@1.0.2”,
“cordova-plugin-whitelist@1.2.1”,
“cordova-plugin-transport-security@0.1.1”,
“cordova-plugin-inappbrowser@1.1.1”,
“de.appplant.cordova.plugin.ema…-composer@0.8.2”,
“cordova-sms-plugin@0.1.6”,
“cordova-plugin-splashscreen@3.0.0”,
“cordova-plugin-x-socialsharing@5.0.10”,
“cordova-plugin-contacts@2.0.1”,
“cordova-plugin-inappbrowser@1.3.0”,
“org.apache.cordova.splashscreen@1.0.0”,
“cordova-plugin-dbcopy”,
https://github.com/devgeeks/Ca…,
“id”: “org.devgeeks.Canvas2ImagePlugin”
https://github.com/wymsee/cord…,
“id”: “cordova-plugin-image-picker”
https://github.com/litehelpers
storage.git#0.7.14”,
“id”: “cordova-sqlite-storage”

Thanks for your support


#2

Do you get any sort of feedback in via remote debugging? What calls bdSQLite() ?


#3

Hi CT14_IT9h thanks for your response

RESPUESTA
The funcion that loads de DB contract.db “bdSQLite()” I called from $ionicPlatform.ready(function () { …
I also tried to put the function out of $ionicPlatform.ready but it didn´t work either.

angular.module(‘starter’, [‘ionic’,’ngIOS9UIWebViewPatch’, ‘starter.controllers’, ‘starter.services’,         ‘ngMaterial’, ‘ngMessages’, ‘ngCordova’])
.    run(function ($ionicPlatform, $cordovaSQLite, $rootScope, $ionicHistory, $state, $mdDialog,     $mdBottomSheet) {


 function bdSQLite() {
window.plugins.sqlDB.copy(“contract.db”, function() {
db = $cordovaSQLite.openDB(“contract.db”);
}, function(error) {
console.error(“Error al copiar bd: ” + error);
db = $cordovaSQLite.openDB(“contract.db”);
});
};

$ionicPlatform.ready(function () {
ionic.Platform.isFullScreen = true;
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}

bdSQLite();

initialRootScope();

    //Checking if view is changing it will go to this function.
    $rootScope.$on(‘$ionicView.beforeEnter’, function () {
    //hide Action Control for android back button.
    hideActionControl();
   // Add custom style ti view.
   $rootScope.customStyle = createCustomStyle($ionicHistory.currentStateName());
    });
    });
……….

#4

Hi there,

Judging by the docs here ( https://github.com/an-rahulpandey/cordova-plugin-dbcopy ) the error function gets called if the database already exists but I see you still assign db if this is the case, what does the console.error line actually return? (if you debug the app via safari/chrome do you get any additional feedback?

Where is db first defined?


#5

This is how the BD is defined

var db = null; //Use for SQLite database.

angular.module('starter', ['ionic','ngIOS9UIWebViewPatch', 'starter.controllers', 'starter.services',     'ngMaterial', 'ngMessages', 'ngCordova'])
  .run(function ($ionicPlatform, $cordovaSQLite, $rootScope, $ionicHistory, $state, $mdDialog,     $mdBottomSheet) {

#6

Here is the console.error :

ionic $ 0 915545 error Uncaught TypeError: Cannot read property ‘sqlDB’
of undefined, http://localhost:8100/js/app.js, Line: 65
1 927381 error TypeError: Cannot read property ‘transaction’ of null
at Object.execute (http://localhost:8100/lib/ngCordova/dist/ng-cordova.js:63
78:11)
at Object.all (http://localhost:8100/templates/application-storage/sqLite/js
/services.js:48:28)
at Scope.$scope.getContractList (http://localhost:8100/templates/application
-storage/sqLite/js/controllers.js:102:39)
at http://localhost:8100/templates/application-storage/sqLite/js/controllers
.js:91:20
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:25050:28
at completeOutstandingRequest (http://localhost:8100/lib/ionic/js/ionic.bund
le.js:13732:10)
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:14112:7
2 945645 error TypeError: Cannot read property ‘transaction’ of null
at Object.execute (http://localhost:8100/lib/ngCordova/dist/ng-cordova.js:63
78:11)
at Object.all (http://localhost:8100/templates/application-storage/sqLite/js
/services.js:48:28)
at Scope.$scope.getContractList (http://localhost:8100/templates/application
-storage/sqLite/js/controllers.js:102:39)
at http://localhost:8100/templates/application-storage/sqLite/js/controllers
.js:91:20
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:25050:28
at completeOutstandingRequest (http://localhost:8100/lib/ionic/js/ionic.bund
le.js:13732:10)
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:14112:7
3 950693 error TypeError: Cannot read property ‘transaction’ of null
at Object.execute (http://localhost:8100/lib/ngCordova/dist/ng-cordova.js:63
78:11)
at Object.all (http://localhost:8100/templates/application-storage/sqLite/js
/services.js:48:28)
at Scope.$scope.getContractList (http://localhost:8100/templates/application
-storage/sqLite/js/controllers.js:102:39)
at http://localhost:8100/templates/application-storage/sqLite/js/controllers
.js:91:20
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:25050:28
at completeOutstandingRequest (http://localhost:8100/lib/ionic/js/ionic.bund
le.js:13732:10)
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:14112:7