2

我想在 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 并且我在物理设备上运行它但仍然无法工作

4

5 回答 5

1

the code is working well, it seems the error comes from a problem in the controller.

于 2014-12-29T09:05:03.330 回答
1

下面的代码对我有用。

我不知道为什么,但是您必须将数据库创建代码放在onReady方法中,然后再if声明如下:

.run(function($ionicPlatform,$cordovaSQLite) {
  $ionicPlatform.ready(function() {  

    db = $cordovaSQLite.openDB({name:"my.db", location:1});              
          $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS testTable(id integer primary key, col1 text,col2 tex)");

    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }                        
  });
于 2016-05-03T13:45:16.707 回答
0

$ionicPlatform.ready() 必须先触发,然后才能使用cordova。

于 2016-05-04T03:15:05.180 回答
0

这确实有效,感谢musakkhiranuj

db = $cordovaSQLite.openDB({name: "dbname.db", location: 1});

location:1很重要。

于 2017-04-29T20:33:52.300 回答
0

您应该使用以下代码。

db = $cordovaSQLite.openDB({name: "dbname.db", location: 1});

它会起作用的。

于 2015-10-05T10:12:36.917 回答