0

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`');

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.