Insert data from a JSON into a Sqlite database with Angularjs

Asked

Viewed 198 times

0

I’m trying to enter the data I took from my API, in the SQLITE database of my app, I already searched a lot but besides not being able to do also I could not understand how I can do this.

Follow the code of mine 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 even used the stringify to see if it would be easier to insert, but I could not do with the examples I found and much less with the tests I did.

1 answer

0

I figured it out, I don’t 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;
      });
    }
  }

})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.