Web SQL SELECT 事务返回值

发布于 2024-12-10 19:38:19 字数 874 浏览 0 评论 0原文

我正在尝试调用一个从我的 Web SQL 数据库中选择值的函数。我想将所选值返回到父函数内的变量中。但是,该变量始终返回空白,无论它是否是全局的。

正如您将能够看到 selectRow 函数内的 console.log 记录了数据库查询中的正确值,但 console.log 在 initDB 函数中显示为空白。

我还注意到空白日志显示在 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;
}

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.

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.

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.

/** 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔书几行 2024-12-17 19:38:19

您可以更改 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);
   });
} 

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:

/** 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);
   });
} 
梦亿 2024-12-17 19:38:19

这很棘手,因为你的回复有延迟
返回数据之前需要等待SQL响应,
这就是为什么需要传递回调函数

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

给我一枪 2024-12-17 19:38:19

请参阅网站: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);
   });

}

与以下 HTML 相关:

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

以及表格: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', '

A+

]); //fonctionnel un à la fois

A+

See the site: 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 the table: 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', '

A+

]); //fonctionnel un à la fois

A+

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文