离子cordovaSQLite插件不适用于物理设备

时间:2014-12-23 08:25:50

标签: cordova ionic-framework cordova-plugins ionic application-icon

我想在离子中将SQLite数据添加到我的移动应用程序中我在以下站点https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/中一步一步地遵循了教程,但它仍然无法在物理设备上工作,任何建议。 在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的表,在我的登录控制器中,我将向人员表插入一行

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}}但是当我在浏览器上运行时我得到了followin错误无法读取未定义的属性'openDatabase'并且我在物理设备上运行它但仍然无法正常工作

5 个答案:

答案 0 :(得分:1)

代码运行良好,似乎错误来自控制器中的问题。

答案 1 :(得分: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();
    }                        
  });

答案 2 :(得分:0)

您应该使用以下代码。

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

它会起作用。

答案 3 :(得分:0)

$ ionicPlatform.ready()必须先激活才能使用cordova。

答案 4 :(得分:0)

这实际上有效,感谢musakkhiranuj

gc

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

相关问题