vendredi 22 avril 2016

how to insert data into SQLite using ionic

I am new to ionic.I want to add data into SQLite which is coming from remote server. I have successfully populated data into list.so how can i store this data into sqlite. here is my code. how do i pass this data to query.I am unable to do this.

service.js

angular.module('starter.service',[]).
  factory('userServices',['$http',function($http){
    var users = [];

    return {
      get: function(){
        return $http.get("http://xxxxxxxxx-info").then(function(response){
          users = response.data;

          return users;

        });
      },
      remove:function(content){
        users.splice(users.indexOf(content),1);
      },
      getUser:function(chatId)
      {
        for(var i=0; i<users.length;i++){
          if(users[i].content_id === parseInt(chatId)){
            return users[i];

          }
        }
        return null;
      }
    }
  }]);

controller.js

 angular.module('shoppingPad.controller', [])


      .controller('ChatCtrl', function ($scope, userServices, $ionicModal, $cordovaSQLite) {
        console.log('inside controller');
       userServices.get().then(function (users) {
          //users is an array of user objects
          $scope.contents = users;
          console.log($scope.contents);
          var query = "INSERT INTO content (content_id, display_name) VALUES (?,?)";
          $cordovaSQLite.execute(db, query, [users.content_id, users.display_name]).then(function (res) {
            alert(res);
            alert('Inserted');
          }, function (e) {
            alert('Error:' + e.message);
          });
        });

Aucun commentaire:

Enregistrer un commentaire