Javascript Web SQL SELECT 事务返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7816509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Web SQL SELECT transaction return value
提问by Tyler Buchea
I am trying to call a function that SELECTS values from my Web SQL database. I would like to return the SELECTED values into a variable inside the parent function. But, the variable always returns blank, wether it is global or not.
我试图调用一个从我的 Web SQL 数据库中选择值的函数。我想将 SELECTED 值返回到父函数内的变量中。但是,变量总是返回空白,无论它是否是全局的。
As you will be able to see the console.log inside the selectRow function logs the correct values from the the database query, but the console.log shows up blank in the initDB function.
正如您将能够看到 selectRow 函数中的 console.log 记录了来自数据库查询的正确值,但 console.log 在 initDB 函数中显示为空白。
I have also noticed that the blank log shows up before the log inside the selectRow function. I have found forums where people are talking about database transactions being asynchronous. I understand that this is why my variable being returned is blank. However, after beating my head against the wall many times I still can't find a way to work around this asynchronous issue.
我还注意到空白日志出现在 selectRow 函数中的日志之前。我发现论坛上的人们都在谈论数据库事务是异步的。我明白这就是为什么我返回的变量是空白的。然而,在多次将我的头撞到墙上之后,我仍然找不到解决这个异步问题的方法。
/** 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;
}
回答by nnnnnn
You could change your selectRow()
function to accept a callback as a parameter, which it will call with the result rather than returning the result:
您可以更改您的selectRow()
函数以接受回调作为参数,它将使用结果调用而不是返回结果:
/** 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);
});
}
回答by Lucas Matos
this is tricky because you have a delayed response you need to wait the SQL response before return data, that's why need to pass a callback function
这很棘手,因为您有延迟响应,您需要在返回数据之前等待 SQL 响应,这就是为什么需要传递回调函数
回答by Alain
See the site: groups.google.com/forum/?fromgroups#!topic/phonegap/YCRt2HducKg
查看网站: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:
与以下 HTML 相关:
Unité: <select name="filtreUniteSelect" id="filtreUniteSelect" ></select><br/>
with the table: Unites
与表:Unites
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+
A+