开发者

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

开发者 https://www.devze.com 2023-03-28 06:07 出处:网络
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):

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. 开发者_StackOverflow中文版
  7. Check to see what data is available in each table B; if it's empty, pre-populate it with default values
  8. Check to see what data is available in each table C; if it's empty, pre-populate it with default values
  9. 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!


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.

0

精彩评论

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

关注公众号