Return Websql query in a global variable?

Asked

Viewed 70 times

0

I am not being able to return the values for Sql query Only within the function

$(function () {

var sincronizado = 'no';
db.transaction(function(tx) {
    tx.executeSql('SELECT * FROM quilometragem WHERE veiculo=?', [id], function (tx, resultado) {                  
        var rows = resultado.rows[0];
        //Aqui retorna o ID da consulta
        alert(rows.id);
        console.log(rows.id);                
    });

        //Aqui não retorna o ID da consulta
        alert(rows.id);
        console.log(rows.id);

}, null);
       //Aqui não retorna o ID da consulta
        alert(rows.id);
        console.log(rows.id);
});  

I need to return the Rows variable in another Function, to send via ajax for the PHP server and aramazenar in mysql.

I search the Websql database data Local and command via ajax to mysql.

1 answer

0

Welcome to Stackoverflow, well let’s go down the stretch where you say:

I need to return the Rows variable in another Function, to send via ajax for the PHP server and aramazenar in mysql.

you have already answered your question, if you already have the result of the query and already have a function that processes this data just call this function passing the result as argument.

See the code below:

function funcao_que_faz_ajax (rows) {
      alert("função que faz ajax chamada.\nVeja o console para mais detalhes.");
      console.log(rows);
}

db.transaction(function(tx) {
    tx.executeSql('SELECT * FROM quilometragem WHERE veiculo=?', [car], function (tx, resultado) {                  
        funcao_que_faz_ajax(resultado.rows);
    });
}, null);

If you want to follow the default callbacks that the websql uses you can enclose the part that makes access to the bank within a function that receives a callback and you when you need to just call the function passing the other function as argument. In practice it’s very simple see:

function selectCarById(db, id, callback) {
    db.transaction(function(tx) {
        tx.executeSql('SELECT * FROM quilometragem WHERE veiculo=?', [id], function (tx, resultado) {                  
            if(callback) callback(resultado)                     
        });
    }, null);
}

And the call of that function would be:

selectCarById(db, 1, function (resultado) {
    // resultado obtido
    alert("Resultado obtido\nVide console para mais detalhes");
    console.log(resultado.rows);
    // To do.. enviar via ajax 
});

As you are using jQuery you can still use all the power of custom events and react to certain events in your document and create a more uncoupled code yet. See:

$(document).on('veiculos.find', function (event, veiculos) {
    alert("Veículos Recebido.\nVeja o console para mais detalhes");
    console.log(veiculos);
    // To do.. enviar via ajax 
});

db.transaction(function(tx) {
    tx.executeSql('SELECT * FROM quilometragem WHERE veiculo=?', [id], function (tx, resultado) {                  
        $(document).trigger('veiculos.find', resultado.rows);                 
    });
}, null);

Which of these 3 ways you should use?

Well I particularly like the events, because your code is totally uncoupled and in the future if you need to do some more with the result of the query just listen to the event again and follow the logic and you do not need to touch code that is already working.

This way your code is "Open for inclusion and closed for editing", but it’s that thing right.... You decide, see which of the ways your code best fits.

I hope I helped you.

Browser other questions tagged

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