I cannot enter data into the sqlite using javascript

Asked

Viewed 1,117 times

5

I’m creating a smartphone app using Cordova for a college project. The App consists of a simple patient registration. I’m having trouble recording data in the database.

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 strange thing is that the successful callback function is executed, that is, apparently there is no error in the Insert.

also took care to test each command in a sqlite database editor and they 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 no longer record.

Note: I also took care to recreate the table. also tried to create a new bank graudecoma2 and also a new table.

1 answer

3


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

Below is a link with frequently asked questions about Sqllite, only in English, it may help you in the future Sqllite

  • 1

    Gee, that’s right! Thank you very much!

  • @Marcusvbp If the answer answers your question, mark it as solved to help other people with similar questions can view the resolution better.

Browser other questions tagged

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