Faizan Ali
Faizan Ali

Reputation: 509

Open database before main controller is called in Ionic Sqlite in Ionic,ngCordova (AngularJS)?

I have a function init DB

this.db = $cordovaSQLite.openDB({ name: "pasa.db" });
$cordovaSQLite.execute(this.db,'CREATE TABLE IF NOT EXISTS dashboard (id integer primary key, orders_open integer,orders_complete integer,orders_all integer,alerts integer)');
$cordovaSQLite.execute(this.db,'CREATE TABLE IF NOT EXISTS orders (reference text primary key not null, first_name text,last_name text,delivery_address_country_code text,state text,merchant_price text)')
$cordovaSQLite.execute(this.db,'CREATE TABLE IF NOT EXISTS products (permalink text primary key not null, title text,active text)')
$cordovaSQLite.execute(this.db,'CREATE TABLE IF NOT EXISTS product_details (permalink text primary key not null, details text)')
$cordovaSQLite.execute(this.db,'CREATE TABLE IF NOT EXISTS order_details (permalink text primary key not null, details text)')

I want to have this function executed before my code execution or resolve in router is called.

$ionicPlatform.ready(function() {
DatabaseService.initDB();
if (window.StatusBar) {
  // org.apache.cordova.statusbar required
  StatusBar.styleDefault();
}

Can you please help me in this regard where my init db is called before anything else or it is a blocking call

Upvotes: 3

Views: 2553

Answers (2)

leopiazzoli
leopiazzoli

Reputation: 127

I'm solved this problem as well:

app.js:

$ionicPlatform.ready(function () { 
    db = $cordovaSQLite.openDB("my.db");
    alert("HERE 1");
});

controller:

$ionicPlatform.ready(function () {
    alert("HERE 2");
    // SELECT 
});

Thus alert 1 comes before alert 2

Upvotes: 0

Sajin M Aboobakkar
Sajin M Aboobakkar

Reputation: 4229

I got the same problem,now its working fine

app.js

var db = null;
angular.module('moduleName',
     ['ionic',
      'ngCordova',
      'db.service'
     ]
    )
    .run(function($ionicPlatform,$cordovaSQLite,DB) {
        $ionicPlatform.ready(function() {
           /*calling service function for initialise database*/
           DB.init();
           if(window.cordova && window.cordova.plugins.Keyboard) {
              cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
           }
           if(window.StatusBar) {
              StatusBar.styleDefault();
           }
    });
})

db.service.js

angular.module('db.service',
    [
     'db.config',
     'ngCordova'
    ]
)
 /* DB service definition goes here*/
.factory('DB', function($q,DB_CONFIG,$cordovaSQLite) {
    var self = this;
    self.db = null;
    /* init function*/
    self.init = function() {
        self.db = $cordovaSQLite.openDB(DB_CONFIG.name+".db");
        angular.forEach(DB_CONFIG.tables, function(table) {
            var columns = [];
            angular.forEach(table.columns, function(column) {
                columns.push(column.name + ' ' + column.type);
            });
            var dbQuery = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + columns.join(',') + ')';
            $cordovaSQLite.execute(self.db,dbQuery)
                .then(
                    function(res) {
                        alert("Tables are created");
                    },function (err) {
                        alert(err);
                    }
                );
        });
    };
return self;
})

db.config.js

/*database configuration goes here,eg:list of tables with columns*/
angular.module('db.config', [])
    .constant('DB_CONFIG', {
        name: 'my',
        tables: [
            {
                name: 'people',
                columns:[
                            {name: 'id', type: 'integer primary key'},
                            {name: 'firstname', type: 'text'},
                            {name: 'lastname', type: 'text'}
                ]
            }
        ]
    });

Upvotes: 3

Related Questions