我想第一次使用带有 cordova 框架的 sqlite 。正如我读过的教程,我应该像这样使用 ngcordova:
var db = null;
app.controller('mainCtrl', function ($scope, $ionicSideMenuDelegate) {
$scope.toggleLeft = function () {
$ionicSideMenuDelegate.toggleLeft();
}
$scope.toggleRight = function () {
$ionicSideMenuDelegate.toggleRight();
}
})
.controller('home_ctrl', function ($scope, $cordovaSQLite) {
db = $cordovaSQLite.openDB({name: "my.db"});
//db = $window.opendb({name: "my.db"});
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS people(id integer primary key, firstname text, lastname text)")
})
;
当我运行此代码时,出现错误:
TypeError: Cannot read property 'openDatabase' of undefined
在一些类似的文章中:如何将 ngCordova sqlite 服务和 Cordova-SQLitePlugin 与 Ionic Framework 结合使用?推荐使用这样的命令:ionic start myApp sidemenu
我不知道它是什么,我只是使用 cordova 命令,例如:cordova run android
or cordova create my_project
我应该怎么做才能从我的 cordova 项目中运行创建表?
你可以看看这个教程:
https://www.thepolyglotdeveloper.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/
当您看到此错误时:
TypeError: Cannot read property 'openDatabase' of undefined
发生这种情况有以下几个原因之一:
- 你没有包装
$cordovaSQLite
中的方法$ionicPlatform.ready()
功能。
- 您正在尝试从网络浏览器测试此本机插件。
- 您实际上尚未将基本 SQLite 插件安装到您的项目中。
此错误最常见的原因是 #1 和 #2。原生插件,必须在应用程序确认就绪后才能使用,因此$ionicPlatform.ready()
方法。由于本机插件使用本机代码,因此您无法从网络浏览器测试它们。
阅读我链接的教程,因为它应该对您有帮助。
Regards,
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)