I can not insert data into sqlite using javascript

5

I'm creating a smartphone app using Cordova for a college project. the App consists of a simple register of patients. I'm having trouble writing data to the bank.

Initially the recording was working, but I forgot to include an id field with auto increment in the table, and from there nothing worked. The strangest thing is that the successful callback function is executed, ie apparently there is no error in the insert.

I was also careful to test each command in an sqlite database editor and all worked correctly.

The Javascript code I'm using is below:

// Criando/abrindo o banco
var db = openDatabase("graudecoma", "1.0", "base de dados da aplicacao", 200000);

// criando a tabela caso ela não exista
    db.transaction(function(transaction){
        transaction.executeSql('CREATE TABLE IF NOT EXISTS pacientes ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "nome" TEXT NOT NULL, "abertura_ocular" INTEGER NOT NULL, "resposta_motora" INTEGER NOT NULL, "resposta_verbal" INTEGER NOT NULL, "diagnostico" TEXT)', [], null, db.onError);
    })

// função callback de erro
db.onError = function(transaction, e) {
  alert("Aconteceu um erro: " + e.message);
  console.log(e.message);
}

// função de callback de sucesso de insert
db.onSuccess = function(transaction, e) {
  alert("Dados Gravados com Sucesso!");
  console.log(e);
}

// função temporaria que lista resultados
db.getResults = function (transaction, r) {
    console.log('deu certo!');
    console.log(r);

   for(var i = 0; i < r.rows.length; i++){
       console.log(r.rows.item(i)[['id']]);
       console.log(r.rows.item(i)[['nome']]);
       console.log(r.rows.item(i)[['diagnostico']]);
   }
}

// aqui vai o insert
db.transaction(function(transaction){
  transaction.executeSql("INSERT INTO pacientes(nome, abertura_ocular, resposta_motora, resposta_verbal, diagnostico) VALUES(?, ?, ?, ?, ?)", [$('#nome').val(), $('input[name="sintoma-1"]:checked').val(), $('input[name="sintoma-2"]:checked').val(), $('input[name="sintoma-3"]:checked').val(), $('#diagnostico').val()], db.onSuccess, db.onError);
})

// consulta no banco
db.transaction(function(transaction){
    transaction.executeSql("SELECT * FROM pacientes", [], db.getResults, db.onError);
})

As I said initially, it was working, but after I changed the CREATE TABLE to include the id I could not write anymore.

Note: I was also careful to re-create the table. I also tried to create a new graudecoma2 bank and also a new table.

    
asked by anonymous 23.04.2014 / 17:53

1 answer

3

Your problem is simpler than you think, remove AUTOINCREMENT , because every primary key will default to AUTOINCREMENT

Below is a link to frequently asked questions about SQLLite, but in English, it might help you in the future SqlLite

    
25.04.2014 / 15:34