SQLite with Javascript does not work

2

I'm getting SQLite now, trying to deploy in a mobile application, I did this test based on some web codes but I can not return a record of the data successfully, until it does the select but it returns 0 records, as the snippet shows : alert(r.rows.length);

var db = openDatabase("appLocal", "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 tb_teste ("teste" TEXT)', [], null, db.onError);
    alert("criou");
});

// aqui vai o insert
db.transaction(function(transaction){    
  transaction.executeSql("INSERT INTO tb_teste(teste) VALUES('testex');", db.onSuccess, db.onError);
    alert("inseriu");
});

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

// função callback de erro
db.onError = function(transaction, e) {
  alert("Aconteceu um erro!");
  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);
    alert(r.rows.length);

   for(var i = 0; i < r.rows.length; i++){
       alert(r.rows.item(i)[['teste']]);
       alert("rodou");
   }
}
    
asked by anonymous 11.02.2016 / 07:39

1 answer

0

The problem is happening in the call of the transaction.executeSql function to insert data into the table.

This function does not accept parameters of callback for error or success (tested in Google Chrome, but for other browsers, it may work differently).

The command line to do the INSERT should look like this:

transaction.executeSql("INSERT INTO tb_teste(teste) VALUES('testex');")

or so:

transaction.executeSql("INSERT INTO tb_teste(teste) VALUES(?);", ['testex']);


See working in jsfiddle


A tip:

According to the Web SQL Database - W3C documentation, this API (SQLite) is deprecated and its use is not recommended.

To store data on the client, you can use:

11.02.2016 / 14:11