mattstuehler
mattstuehler

Reputation: 9312

HTML5 local SQL database - How to execute a sequence a series of database transactions where order matters?

I'm working on an HTML5-based web application that will make use of a local SQL database.

When the app starts up, it needs to do the following (simplified, but roughly correct):

  1. Check to see if the database exists; if not, create it
  2. Check to make sure if table A exist; if not, create it
  3. Check to make sure if table B exist; if not, create it
  4. Check to make sure if table C exist; if not, create it
  5. Check to see what data is available in each table A; if it's empty, pre-populate it with default values
  6. Check to see what data is available in each table B; if it's empty, pre-populate it with default values
  7. Check to see what data is available in each table C; if it's empty, pre-populate it with default values
  8. Render the UI based on the data retrieved in steps 5-7

As I understand it, each executeSQL transaction is asynchronous.

In this case - certain steps must be complete before other steps can be executed. In other words, #1 must be complete before anything else can happen. Next, #2, #3, and #4 don't need to be executed serially - but #2 needs to be complete before #5, #3 needs to be complete before #6, etc.

This must be a pretty common challenge. So, what's the best way to code a series of dependent SQL transactions like this?

For what it's worth - this app is targeting iPhones exclusively, so I don't need to worry about browser quirks, or check for the existence of this functionality. But, a cross-browser solution is always preferable.

Many thanks in advance!

Upvotes: 1

Views: 439

Answers (1)

JB Nizet
JB Nizet

Reputation: 691913

executeSql takes a data handler callback function as argument. The data handler function is executed once the query has executed and gives you access to the result set. Just execute the next query in the data handler of the previous query.

Upvotes: 2

Related Questions