Loading The Database Object When Your Stack Opens
If you are storing your Database object in a Revolution button across sessions then you will need to tell SQL Yoga to load the Database object each time your project is opened.
This lesson will show you how to load the Database object from the button that you have saved it in.
dbobject_createFromObject
SQL Yoga provides a command that will create a Database object from the control that you have saved it in. The command is called dbobject_createFromObject and you pass a reference to the control that your Database object is stored in.
Example Script
In this example the dbobject_createFromObject command is called when a card opens and before any SQL Yoga calls are made that interact with the Database object.
Here is an example of what a finished openCard script might look like. Some things to note:
1) A variable is being stored that ensures the code only runs the first time the card is open.
2) Error checking has been added.
==========
Copy & Paste (remember to customize connection settings)
==========
local sInitialized
on openCard
## Only run this code the first time the card opens
if not sInitialized then
## Load Database Object from Button
put the long id of button "Database Object Storage" into theObject
dbobject_createFromObject theObject
put the result into theError
## MySQL Example
if theError is empty then
dbconn_set "host", "localhost"
dbconn_set "username", "root"
dbconn_set "password", empty
dbconn_set "database name", "sql_yoga_test"
try
## If a connection fails then an error is thrown
## Later on this will help you handle this error
## globally in your application by adding your
## own errorDialog handler. For now just wrap
## the dbconn_connect call in try/catch.
dbconn_connect
catch theError
answer "An error occurred while connecting to the database:" && theError
end try
end if
if theError is empty then
## Set flag so code doesn't run again
put true into sInitialized
end if
end if ## end of initialization code
end openCard
0 Comments
Add your comment