开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜