I can not insert data into the SQLite table

1

I want to insert some data into my SQLite table.

Creating the table 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
      }
   }
})
    
asked by anonymous 10.02.2017 / 12:21

2 answers

1

The error was of syntax and it was very difficult to find a comma to more that had in the code. To find out I had to use a function to get the error and print. I would advise everyone to use it when working with SQLite manipulation in JavaScript

Example:

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

    
13.02.2017 / 13:40
0

Note that I've edited your question, formatting SQL , because it makes it easier to understand.

And I noticed that codbarras and embalagem_vol are NUM , and you're passing text in insert , try changing it to 0. id is INTEGER so pass an integer as well.

Test and see if it works.

    
10.02.2017 / 12:48