我正在使用Phonegap
框架。所有 js 文件均已完全加载。
我正在使用以下语句。
var db = window.sqlitePlugin.openDatabase({ name: "MyDB.db" });
但我收到如下错误:
TypeError: Cannot read property 'openDatabase' of undefined
有什么建议吗?
最佳答案
有几点需要了解:
- 数据库事件只能在
onDeviceReady()
方法触发时完成 为此,我将在
modules .run()
方法中打开数据库,如下所示:db = $cordovaSQLite.openDB("my.db");
在编写此行之前,请确保您已添加 SQLite 插件 并在项目中添加 $cordovaSQLite
依赖项。
这是一个例子:
var db = null;
var example = angular.module('starter', ['ionic', 'ngCordova'])
.run(function($ionicPlatform, $cordovaSQLite) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
db = $cordovaSQLite.openDB("my.db");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)");
});
});
希望这对您有用。作为引用,您可以使用 this :
关于angularjs - 无法读取未定义的属性 'openDatabase',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32862851/