I cannot enter data in Sqlite Table

Asked

Viewed 162 times

1

I want to insert some data into my Sqlite table.

Table creation in . app

$cordovaSQLite.execute(db, 
"CREATE TABLE IF NOT EXISTS tbprodutos(
 id                   INTEGER PRIMARY KEY,
 codbarras            NUM,
 desccupom            TEXT,
 grupo                TEXT,
 categoria            TEXT,
 produto              TEXT,
 nome                 TEXT,
 marca                TEXT,
 embalagem_vol        NUM,
 embalagem_uni        TEXT,
 embalagem_desc       TEXT,
 caracteristica_desc1 TEXT,
 caracteristica_info1 TEXT,
 caracteristica_desc2 TEXT,
 caracteristica_info2 TEXT,
 caracteristica_desc3 TEXT,
 caracteristica_info3 TEXT,
 preco                TEXT,
 modificado           TEXT)"
 );

Service where I enter the data (coming from Firebase) in my table:

angular.module('starter.service.tbProdutos', [])

.factory('tbProdutos', function($http, $cordovaSQLite, $localStorage,  $firebaseObject, dbLocal) {
var ref = firebase.database().ref().child("CodBarras");
var obj = $firebaseObject(ref);

return {
  populate: function() {
     obj.$loaded().then(function() {
        console.log("OBJETO: ", obj.Preco);
        var CodBarras = obj.CodBarras;
        var DescCupom = obj.DescCupom;
        var Grupo = obj.Grupo;
        var Categoria = obj.Categoria;
        var Produto = obj.Produto;
        var Nome = obj.Nome;
        var Marca = obj.Marca;
        var Embalagem_Vol = obj.Embalagem_Vol;
        var Embalagem_Uni = obj.Embalagem_Uni;
        var Embalagem_Desc = obj.Embalagem_Desc;
        var Caracteristica_Desc1 = obj.Caracteristica_Desc1;
        var Caracteristica_Info1 = obj.Caracteristica_Info1;
        var Caracteristica_Desc2 = obj.Caracteristica_Desc2;
        var Caracteristica_Info2 = obj.Caracteristica_Info2;
        var Caracteristica_Desc3 = obj.Caracteristica_Desc3;
        var Caracteristica_Info3 = obj.Caracteristica_Info3;
        var Preco = obj.Preco;
        var Modifica = obj.Modificado;

        angular.forEach(obj, function(value, key) {
           console.log(key, value);
        });
        var parameters = [obj.CodBarras, obj.DescCupom, obj.Grupo, 

obj.Categoria, obj.Produto, obj.Nome, obj.Marca, obj.Embalagem_Vol, obj.Embalagem_Uni, obj.Embalagem_Desc, obj.Caracteristica_Desc1, obj.Caracteristica_Info1, obj.Caracteristica_Desc2, obj.Caracteristica_Info2, obj.Caracteristica_Desc3, obj.Caracteristica_Info3, obj.Preco, obj.Modificado];

$cordovaSQLite.execute(db, 
"INSERT INTO tbprodutos
  (id INTEGER primary key
  ,codbarras
  ,desccupom
  ,grupo
  ,categoria
  ,produto
  ,nome
  ,marca
  ,embalagem_vol
  ,embalagem_uni
  ,embalagem_desc
  ,caracteristica_desc1
  ,caracteristica_info1
  ,caracteristica_desc2
  ,caracteristica_info2
  ,caracteristica_desc3
  ,caracteristica_info3
  ,preco
  ,modificado)
VALUES
  ('?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?'
  ,'?')", parameters).then(function(result) {
               console.log("RESULT >> ", result);
            });
            console.log(parameters);
         });
         //var userId = firebase.auth().currentUser.uid; FORMA CORRENTA DE PEGAR O USUARIO CORRENTE
      }
   }
})
  • 1

    If an error appears ideal is you edit your question and present it too.

2 answers

1


The error was syntax and it was very difficult to find an extra comma in the code. To find I had to use a function to catch the error and print. I advise everyone to use when working with manipulation of Sqlite in the Javascript

Example:

$cordovaSQLite.execute(db, "INSERT INTO table () VALUES ()").then(function(result) { console.log(result); }, function(err) { console.log(err); });

0

Notice I edited your question, formatting the SQL, 'cause it makes it easier to understand.

And I realized that the codbarras and embalagem_vol sane NUM, and you’re passing a text on insert, try to change to 0. The id is a INTEGER then spend a whole too.

Test and see if it solves.

  • I changed the field num to text to see if it accepted too and it still didn’t work. Nor does it create the table structure.. so I think the error is time for insertion. I don’t know if you can put a $cordovaSQLite.execute within a $loaded(). The var parameters passes all filled with the above values. Any idea?

Browser other questions tagged

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