Connecting to a Database Using a Connection Object
A Connection object is used by SQL Yoga to connect to a database. You set properties on this object and then call dbconn_connect to open a connection. Let's look at how this is done.
Create a Database Connection Object
To create a Connection object you call dbconn_createObject. You pass in a name that you would like to give to the connection as the 1st parameter (1) and the type of database you are connecting to as the 2nd parameter (2).
You only need to create a Connection object once so you can execute it in the message box if you would like.
==========
Copy & Paste
==========
dbconn_createObject "development", "sqlite"
put the result
Save Database Object
A Connection object is stored inside of a Database object much like a control can be stored inside of a group. Since you have added a new object to the Database object you will need to save it so that the new object is not lost when you quit Revolution. Just call dbobject_save and then save the stack file to disk.
Define Connection Properties
Now that a Connection object exists we can configure it and then connect to the database. You need to configure most of the properties of a Connection object at runtime. SQL Yoga does not store information such as the username and password inside the object.
Edit the card script of the stack you are working on and add an openCard handler.
==========
Copy & Paste
==========
on openCard
## MySQL Example
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 e
answer "An error occurred while connecting to the database:" && e
end try
end openCard
Call openCard Handler
After you finish customizing the connection settings in the openCard handler you can trigger openCard from the Message Box. This will configure your Connection object and connect to the database.
Some Examples
Here are some examples of properties you set when connecting to different types of databases.
## SQLite Example
dbconn_set "file", theFullPathToTheDatabaseFile
## MySQL Example
dbconn_set "host", "localhost"
dbconn_set "username", "root"
dbconn_set "password", empty
dbconn_set "database name", "sql_yoga_test"
## ODBC Example
-- Note: When calling dbconn_createObject you pass in 3 parameters
-- The 2nd is "odbc" and the 3rd is the type of database you are
-- connection too
-- dbconn_createObject "development", "odbc", "sql server"
dbconn_set "dsn", "sqlserver_odbc"
dbconn_set "username", "odbc_user"
dbconn_set "password", empty
## Local Valentina Example
dbconn_set "file", theFullPathToTheDatabaseFile
dbconn_setVendor "valentina", "mac serial number", MyValMacSerialNumber
dbconn_setVendor "valentina", "win serial number", MyValWinSerialNumber
dbconn_setVendor "valentina", "unix serial number", MyValUnixSerialNumber
dbconn_setVendor "valentina", "encryption key", MyEncryptionKey
## Valentina Client Example
dbconn_set "host", "localhost"
dbconn_set "username", "sa"
dbconn_set "password", "sa"
dbconn_set "use ssl", true
dbconn_set "database name", "my database.vdb"
0 Comments
Add your comment