Question

Instead of doing an each loop on a JSON file containing a list of SQL statments and passing them one at a time, is it possible with Safari client side storage to simply wrap the data in "BEGIN TRANSACTION" / "COMMIT TRANSACTION" and pass that to the database system in a single call? Looping 1,000+ statements takes too much time.

Currently iterating one transaction at a time:

$j.getJSON("update1.json",
  function(data){
    $j.each(data, function(i,item){
            testDB.transaction(
                function (transaction) {
                    transaction.executeSql(data[i], [], nullDataHandler, errorHandler);
                }
            );
   });
});

Trying to figure out how to make just one call:

$j.getJSON("update1.json",
  function(data){
            testDB.transaction(
                function (transaction) {
                    transaction.executeSql(data, [], nullDataHandler, errorHandler);
                }
            );
});

Has anybody tried this yet and succeeded?

Was it helpful?

Solution

Every example I could find in the documentation seems to show only one SQL statement per executeSql command. I would just suggest showing an "ajax spinner" loading graphic and execute your SQL in a loop. You can keep it all within the transaction, but the loop would still need to be there:

$j.getJSON("update1.json",
    function(data){
       testDB.transaction(
           function (transaction) {
               for(var i = 0; i < data.length; i++){
                   transaction.executeSql(data[i], [], nullDataHandler, errorHandler);
               }
           }
       );
    }
);

Moving the loop inside the transaction and using the for i = should help get a little more speed out of your loop. $.each is good for less than a 1000 iterations, after that the native for(var = i... will probably be faster.

Note Using my code, if any of your SQL statements throw errors, the entire transaction will fail. If that is not your intention, you will need to keep the loop outside the transaction.

OTHER TIPS

I haven't ever messed with HTML5 database storage (have with local/sessionStorage though) and I would assume that it's possible to run one huge string of statements. Use data.join(separator here) to get the string representation of the data array.

Yes, it is possible to process a whole group of statements within a single transaction with webSQL. You actually don't even need to use BEGIN or COMMIT, this is taken care of for you automatically as long as you make all your executeSql calls from the same transaction. As long as you do this every statement gets included within the transaction.

This makes the process much faster and also makes it so that when one of your statements has an error it rolls back the entire transaction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top