Initializing SQL Yoga At Launch

Now that you have configured a SQL Yoga Database and Connection object we will look at how to initialize your Database object each time the application launches.

Update 'application' Stack Script

Before we walk through the code, update the application stack script by replacing the glxapp_initializeApplication handler with the following RevTalk. After you insert the code we will go through it step by step.

After pasting in the updated code the script tab will display a yellow circle (1). Make sure you compile the script so that the changes are saved (2).

----------
Copy & Paste The Following Code
----------
on glxapp_initializeApplication
  ## THIS HANDLER IS CALLED ONCE THE FRAMEWORK HAS LOADED ALL STACKS, LIBRARIES AND EXTERNALS.
  ## PERFORM ANY INITIALIZATION ROUTINES HERE SUCH AS SETTING FONTS FOR STACKS, navigationArrows, ETC
  	
  ## Examples
  set navigationarrows to false
  if the environment is not "development" then
    set allowinterrupts to false
  end if
  	
  ## Register SQL Yoga
  sqlyoga_register "myemail@myemail.com", \
        "MY_REGISTRATION_KEY"
  put the result into theError
  	
  ## Tell RevDB where the SQLite driver is
  revSetDatabaseDriverPath glxapp_getprop("executable folder") & "/components/externals"
  	
  ## Load the database object from it's storage location
  dbobject_createFromObject the long id of \
        button "SQL Yoga Database Object Storage" of me
  	
  ## Point Connection object to SQLite database file
  put glxapp_getprop("executable folder") & "/components/to-do.sqlite" into theFile
  dbconn_set "file", theFile
  	
  dbconn_connect
  put the result into theError
  	
  if theError is not empty then
    answer "Unable to initialize program (" & theError & "). Application will quit."
    if the environment is not "development" then
      ## returning false tells framework that initialization failed and app
      ## should quit.
      return false
    end if
  end if
end glxapp_initializeApplion

Loading The Database Object When Application Opens

Each time your application launches you need to load the Database object that is stored in the SQL Yoga Database Object Storage button into memory so that SQL Yoga has access to all of the information. You do this by calling dbobject_createFromObject (1). You only need to do this one time at the beginning of each application session.

Point The Connection Object to the SQLite Database File and Connect

We need configure Connection object properties each time the application launches. SQL Yoga does not permanently store properties such as the path to the SQLite file or the host, username and password for a MySQL database.

Even though the to-do.sqlite database file is stored alongside the application you won't know the full path to the database file until the application launches. We determine this path using an application framework property to locate the file and then setting the file property of the Connection object to it. Now is also a good time to connect to the database. By doing so you can check for errors and alert the user accordingly.

When working with the GLX Application Framework the executable folder property returns the root folder where application files are stored so you can use it to find the path to the to-do.sqlite file.

That's It!

Now you have all of the pieces in place in order to use SQL Yoga in your application. Let's go have some fun by creating some GUI controls that allow us to interact with the data.

0 Comments

Add your comment

E-Mail me when someone replies to this comment