在 Ionic、ngCordova (AngularJS) 中的 Ionic Sqlite 中调用主控制器之前打开数据库?

2024-04-26

我有一个函数 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)')

我想在调用路由器中的代码执行或解析之前执行此函数。

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

您能否在这方面帮助我,在这方面我的 init db 在其他任何事情之前被调用,或者它是一个阻塞调用


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

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

数据库服务.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;
})

数据库配置.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'}
                ]
            }
        ]
    });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Ionic、ngCordova (AngularJS) 中的 Ionic Sqlite 中调用主控制器之前打开数据库? 的相关文章

随机推荐