dimanche 23 août 2015

SQLite Database for Ionic

I am having trouble doing CRUD operations in SQLiteDatabase in ionic framework. This is my main "index.html" page, where I am displaying my div by replacing another div using "replaceWith()", which I learned recently!

<meta charset="utf-8">
<meta name="viewport"
      content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Still Learning</title>

<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="js/jquery-1.11.3.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>
<script>
$(document).ready(function(){
    $("#insertbtn").click(function(){
        $(".toshow").replaceWith('<div class="toshow" ng-controller="MyController">\
          <div class="list">\
          <label class="item item-input">\
            <input type="text" placeholder="Full Name" ng-model="fullname">\
          </label>\
          <label class="item item-input">\
            <input type="text" placeholder="Address" ng-model="address">\
          </label>\
          <label class="item item-input">\
            <input type="text" placeholder="Email" ng-model="email">\
          </label>\
          <label class="item item-input">\
            <input type="number" placeholder="Age" ng-model="age">\
          </label>\
          <label class="item item-input">\
            <input type="password" placeholder="Password" ng-model="password">\
          </label>\
          <label class="item item-input">\
            <textarea placeholder="Details" ng-model="details"></textarea>\
          </label>\
          <br /><button class="button button-outline button-positive" ng-click="insert(fullname, address, email, age, password, details)">INSERT</button>\
          </div><p>{{ statusMessage }}</p>\
        </div>');
    });

});
</script>

Now here, in this replaced div, I add some values and want to insert that into the SQLite Database!

And this is my app.js file:

    var db=null;
    angular.module('starter', ['ionic', 'ngCordova'])
    .run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
         if (window.cordova && window.cordova.plugins.Keyboard) {
          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
          cordova.plugins.Keyboard.disableScroll(true);
        }
        if (window.StatusBar) {
          StatusBar.styleDefault();
        }

        db = $cordovaSQLite.openDB("sample.db");
        $cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS User(id INTEGER PRIMARY KEY AUTOINCREMENT, fullname TEXT, address TEXT,email TEXT, age INTEGER, password TEXT, details TEXT)');
            });
    })

    .controller('MyController', function($scope, $cordovaSQLite) {
    $scope.insert = function(fullname, address, email, age, password, details) {
    $cordovaSQLite.execute(db, 'INSERT INTO User (fullname, address, email, age, password, details) VALUES (?,?,?,?,?,?)', [fullname, address, email, age, password, details])
        .then(function(result) {
            $scope.statusMessage = "Data saved successful, cheers!";
        }, function(error) {
            $scope.statusMessage = "Error on saving: " + error.message;
        })
}
    })        

There is no error watsoever, but my data is not being inserted!! Can someone tell me whats my mistake here? THankx in advance.....

Aucun commentaire:

Enregistrer un commentaire