在 angular 工厂中调用 opendatabase 时,它抛出 security_err dom 异常 18 错误
When called opendatabase in angular factory, it thows security_err dom exception 18 error
我正在用 vs2015 开发 Angularjs 应用程序。
我想通过工厂函数在 websql 中打开数据库。在 ripple 模拟器中测试应用程序时它完美运行(在 android OS 4.4 及更高版本中也完美运行)但是在 android OS 4.2 及更低版本中测试应用程序时它抛出security_err dom exception 18
。
更奇怪的是,当同一个数据库时,我在简单的 java 脚本文件中打开它在 ripple 和所有设备中也能完美运行。
这是我的工厂代码:
angular.module('InnkeyAlert.services', ['InnkeyAlert.config'])
// DB wrapper
.factory('DB', function ($q, DB_CONFIG) {
try {
var self = this;
self.db = null;
self.init = function () {
alert("DB init start!");
// Use self.db = window.sqlitePlugin.openDatabase({name: DB_CONFIG.name}); in production
self.db = window.openDatabase(DB_CONFIG.name, '1.0', 'database', -1);
alert("DB open!");
angular.forEach(DB_CONFIG.tables, function (table) {
var columns = [];
angular.forEach(table.columns, function (column) {
columns.push(column.name + ' ' + column.type);
});
var query = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + columns.join(',') + ')';
self.query(query);
//console.log('Table ' + table.name + ' initialized');
});
alert("table created !");
};
return self;
} catch (e) {
//alert("Db factory: "+e.message);
}
})
这是我的 index.js 文件代码:
var app = {
// Application Constructor
initialize: function () {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function () {
alert("hi");
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function () {
app.receivedEvent('deviceready');
alert("Device ready end...");
try {
angular.injector(['InnkeyAlert']).get('DB').init();
alert("inti db called...");
} catch (e) {
alert("index 101: " + e.message);
}
},
// Update DOM on a Received Event
receivedEvent: function (id) {
}
};
这是 Android 低于 4.4 的版本中的问题。已回答 here:
您可以在 WebView 中使用来自 file:// URL 的 Web SQL。科尔多瓦人设法做到了。只有三件事是您必须做的:
1) 调用 setDatabaseEnabled()(废话):
2) 设置数据库文件路径。是的,这在 Android 4.4 中已弃用,但如果您想避免 pre-Kitkat 中的 DOM 异常 18,则需要这样做:
3) 设置 onExceededDatabaseQuota 处理程序。是的,它在 Android 4.4 中已弃用,等等等等。
我正在用 vs2015 开发 Angularjs 应用程序。
我想通过工厂函数在 websql 中打开数据库。在 ripple 模拟器中测试应用程序时它完美运行(在 android OS 4.4 及更高版本中也完美运行)但是在 android OS 4.2 及更低版本中测试应用程序时它抛出security_err dom exception 18
。
更奇怪的是,当同一个数据库时,我在简单的 java 脚本文件中打开它在 ripple 和所有设备中也能完美运行。
这是我的工厂代码:
angular.module('InnkeyAlert.services', ['InnkeyAlert.config'])
// DB wrapper
.factory('DB', function ($q, DB_CONFIG) {
try {
var self = this;
self.db = null;
self.init = function () {
alert("DB init start!");
// Use self.db = window.sqlitePlugin.openDatabase({name: DB_CONFIG.name}); in production
self.db = window.openDatabase(DB_CONFIG.name, '1.0', 'database', -1);
alert("DB open!");
angular.forEach(DB_CONFIG.tables, function (table) {
var columns = [];
angular.forEach(table.columns, function (column) {
columns.push(column.name + ' ' + column.type);
});
var query = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + columns.join(',') + ')';
self.query(query);
//console.log('Table ' + table.name + ' initialized');
});
alert("table created !");
};
return self;
} catch (e) {
//alert("Db factory: "+e.message);
}
})
这是我的 index.js 文件代码:
var app = {
// Application Constructor
initialize: function () {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function () {
alert("hi");
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function () {
app.receivedEvent('deviceready');
alert("Device ready end...");
try {
angular.injector(['InnkeyAlert']).get('DB').init();
alert("inti db called...");
} catch (e) {
alert("index 101: " + e.message);
}
},
// Update DOM on a Received Event
receivedEvent: function (id) {
}
};
这是 Android 低于 4.4 的版本中的问题。已回答 here:
您可以在 WebView 中使用来自 file:// URL 的 Web SQL。科尔多瓦人设法做到了。只有三件事是您必须做的:
1) 调用 setDatabaseEnabled()(废话):
2) 设置数据库文件路径。是的,这在 Android 4.4 中已弃用,但如果您想避免 pre-Kitkat 中的 DOM 异常 18,则需要这样做:
3) 设置 onExceededDatabaseQuota 处理程序。是的,它在 Android 4.4 中已弃用,等等等等。