Insert data from a JSON into a Sqlite database with AngularJS

0

I'm trying to insert the data I get from my API, into the SQLITE database of my app, I already researched a lot but besides not being able to do it, I could not figure out how to do this.

Here is the code for my service :

app.service('CategoriesService', function($http, $q) {

  var url = 'http://meusite.com.br/api/';

  return {
    allCategories: function() {
      return $http.get(url).then(function(response) {
        var json = JSON.stringify(response.data);
        console.log(JSON);
        return response.data;
      });
    }
  }

})

I started using stringify to see if it was easier to insert, but I could not do with the examples I found, let alone the tests I did.

    
asked by anonymous 23.09.2016 / 22:00

1 answer

0

I've been able to resolve it, I do not know if it's the right way to do it, but it's working.

app.service('CategoriesService', function($http, $q) {

  var url = 'http://meusite.com.br/api/';

  return {
    allCategories: function() {
      return $http.get(url + 'categories').then(function(response) {
        var result = response.data;

        result.forEach(function(category) {
          db.transaction(function(tx) {
            var query = "INSERT INTO tblCategories (id, category_name) VALUES (?,?)",
                params = [category.category_id, category.category_name];
            tx.executeSql(query, params);
            console.log('Salvo');
          }, function(error) {
            console.log('Erro');
          });
        })

        return response.data;
      });
    }
  }

})
    
24.09.2016 / 01:05