我遇到了同样的问题,现在工作正常
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'}
]
}
]
});