我想在 ionic 中将 SQLite 数据添加到我的移动应用程序中,我已在以下站点https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-中逐步按照教程进行操作框架/但它仍然无法在物理设备上运行,任何建议。在 index.html 我把下面的代码
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ionic/js/angular-ui/angular-ui-router.js"></script>
<script src="lib/ionic/js/angular/angular-resource.js"></script>
<!-- Needed for Cordova/PhoneGap (will be a 404 during development) -->
<script src="js/app.js"></script>
<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>`
在我的 app.js 中,我首先编写了以下代码
var db=null;
var myApp=angular.module('myApp',['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("test.db");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)");
console.log("hello");
})
})
所以我创建了一个名为 people 的表,在我的登录控制器中,我将在 people 表中插入一行
controller('loginController',function($scope,$cordovaSQLite){
var query = "INSERT INTO people (id,firstname, lastname) VALUES (?,?,?)";
$cordovaSQLite.execute(db, query, [1,"khaled", "omar"]).then(function(res) {
$scope.name=res.insertId;
}, function (err) {
$scope.name="error";
});
在 login.html 中我写了 {{name}} 但是当我在浏览器上运行它时出现以下错误 Cannot read property 'openDatabase' of undefined 并且我在物理设备上运行它但仍然无法工作