开发者

How do I get a function to return on SQLite Javascript databases?

开发者 https://www.devze.com 2023-01-29 17:54 出处:网络
I\'m using the following function to execute SQL, it works fine for when no return is needed but if I do a query like \"SELECT * FROM table\" then it won\'t return anything. The function is:

I'm using the following function to execute SQL, it works fine for when no return is needed but if I do a query like "SELECT * FROM table" then it won't return anything. The function is:

function executeSQL(query)
{
  return db.transaction(function(q) 
  {    
    return q.开发者_运维技巧executeSql(query, null, 
  function (q, results)
  {
   debug(results);
   return results;
  }, 
  function (q, error)
  {
   debug(error);
  }
 );
  });
}

And the way I'm calling it is:

results = executeSQL('SELECT * FROM `table`');


The transaction function is asynchrounous: it does not wait until the function you gave it as parameter returns, and in fact probably doesn't return anything.

I don't think it's possible to write a wrapper function that returns the result, unless you wait in a loop, which is problematic for JavaScript, and also not advisable.

function executeSQL(query, callback) {
    db.transaction(function(q) {
        q.executeSql(query, null, function (q, results) {
            callback(results);
        }, function (q, error) {
            debug(error);
        });
    });
}

var query = "SELECT 1";
executeSQL(query, function(result) {
    alert(result);
});
alert("When am I?"); // this may get called before alert(result)
0

精彩评论

暂无评论...
验证码 换一张
取 消