SQL SQL SELECT transaction return value

I am trying to call a function that is SELECTING values ​​from my web SQL database. I would like to return SELECTED values ​​to a variable inside the parent function. But a variable always returns empty if it is global or not.

Since you will see that console.log inside the selectRow function registers the correct values ​​from the database query, but console.log is displayed empty in the initDB function.

I also noticed that an empty log appears in front of the log inside the selectRow function. I found forums where people talk about database transactions that are asynchronous. I understand that therefore the returned variable is empty. However, repeatedly beating my head against the wall, I still cannot find a way around this asynchronous problem.

/** Initialize Database **/ function initDB(){ createTable(); var pleaseWork = selectRow("SELECT * FROM planets;"); console.log(pleaseWork); } /** Select Row from Table **/ function selectRow(query){ var result = []; db.transaction(function (tx) { tx.executeSql(query, [], function(tx, rs){ for(var i=0; i<rs.rows.length; i++) { var row = rs.rows.item(i) result[i] = { id: row['id'], name: row['name'] } } console.log(result); }, errorHandler); }); return result; } 
+6
source share
3 answers

You can change your selectRow() function to accept a callback as a parameter, which it will call with the result, rather than returning the result:

 /** Initialize Database **/ function initDB(){ createTable(); selectRow("SELECT * FROM planets;", function(pleaseWork) { console.log(pleaseWork); // any further processing here }); } /** Select Row from Table **/ function selectRow(query, callBack){ // <-- extra param var result = []; db.transaction(function (tx) { tx.executeSql(query, [], function(tx, rs){ for(var i=0; i<rs.rows.length; i++) { var row = rs.rows.item(i) result[i] = { id: row['id'], name: row['name'] } } console.log(result); callBack(result); // <-- new bit here }, errorHandler); }); } 
+17
source

This is difficult because you have a delayed response, you need to wait for the SQL response before the return data, for this you need to pass a callback function

+3
source

See website: groups.google.com/forum/?fromgroups#!topic/phonegap/YCRt2HducKg

 function loadUniteSelectListe() { db.transaction(function (tx) { //populate drop down for unites tx.executeSql('SELECT * FROM Unites', [], function (tx, results) { var len = results.rows.length; var i=0; var txt=""; for (i = 0; i < len; i++){ txt=txt + "<option value="+results.rows.item(i).uniteName + ">" + results.rows.item(i).uniteSymbol + "</option>"; } document.getElementById("filtreUniteSelect").innerHTML=txt; }, null); }); 

}

Related to the following HTML:

 Unité: <select name="filtreUniteSelect" id="filtreUniteSelect" ></select><br/> 

with table: Combines

 CREATE TABLE IF NOT EXISTS Unites (uniteID INTEGER PRIMARY KEY AUTOINCREMENT, uniteName TEXT, uniteSymbol TEXT) tx.executeSql("INSERT INTO Unites(uniteName,uniteSymbol) VALUES(?, ?)", ['heure', 'h']); //fonctionnel un à la fois tx.executeSql("INSERT INTO Unites(uniteName,uniteSymbol) VALUES(?, ?)", ['kilometre', 'km']); //fonctionnel un à la fois tx.executeSql("INSERT INTO Unites(uniteName,uniteSymbol) VALUES(?, ?)", ['dollar', '$']); //fonctionnel un à la fois 

A +

0
source

Source: https://habr.com/ru/post/899584/


All Articles