在Ionic的Ionic Sqlite,ngCordova(AngularJS)中调用主控制器之前打开数据库? [英] Open database before main controller is called in Ionic Sqlite in Ionic,ngCordova (AngularJS)?

查看:118
本文介绍了在Ionic的Ionic Sqlite,ngCordova(AngularJS)中调用主控制器之前打开数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数init DB

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();
}

在这方面我可以帮助我在其他任何事情之前调用我的init数据库,或者它是一个阻塞调用

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

推荐答案


我遇到了同样的问题,现在工作正常

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'}
                ]
            }
        ]
    });

这篇关于在Ionic的Ionic Sqlite,ngCordova(AngularJS)中调用主控制器之前打开数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆