A class for REALbasic
©2004 by Floyd Resler
Do you have the Standard version of REALbasic yet need to
make a mySQL database connection?
Your only solution is to either purchase the Pro version of REALbasic
for an additional $300 or, if you can find one, a mySQL plug-in. Well, not anymore!
Introducing pseudoMySQL,
pseudoMySQL doesnÕt actually make a connection to a mySQL
database itself. Instead, it
communicates to a mySQL database through a PHP script. The class comes with complete
instructions (including instructions on how to activate PHP on your own
computer) and an example project.
Download – 44KB
Below are the instructions included with the class.
The pseudoMySQL class for REALbasic allows you to access a
mySQL database without the need for the pro version of REALbasic or a mySQL
plug-in. It accomplishes this by
communicating with a PHP script.
To use pseudoMySQL, you will need to install two items.
pseudoMySQL.php must be placed on a server that has Apache
configured to run PHP scripts.
Since Mac OS X has Apache, you can place it your local system. However, a couple of modifications to
the Apache configuration file need to be made. Follow these steps:
Remember, you can put pseudoMySQL.php on any server that as Apache confirgured to run PHP
scripts.
pseudoMySQLÕs super class is HTTPSocket. Therefore, you will need to:
Drag a TCP Socket control onto your window and make its
Super pseudoMySQL.
You will need to set some properties before access the mySQL
database. These are:
Once these properties are set, you are ready to go.
One of the first things youÕll probably do is want to
execute a SELECT query. This is
handled with the mysql_select
method. When the mysql_select
method has finished, the results are placed into a records array (i.e. a record set) and the
mysql_selected event occurs. To access each row in the record set,
you will want to use the eof method and
reference the currentRecord
property.
Example:
You execute the following query:
dbase.mysql_selecte(Òselect * from customers where
lastName=ÕSmithÕÓ)
When finished, the mysql_selected event occurs. So, within that event, you could do
this:
while not dbase.eof
ListBox1.AddRow
dbase.currentRecord.value(ÒlastNameÓ)+Ó,
Ò+dbase.currentRecord.value(ÒfirstNameÓ)
wend
(Note there is no MoveNext method).
See the sample project for an expanded example.
There are two ways to update records in the database. You can do it through an UPDATE query
or you can use the mysql_update
method. When either one is
finished, the mysql_updated
event will occur.
If you wish to use the mysql_update method, you must pass to
it the following parameters:
Example:
dim fields as dictionary
fields=new dictionary
fields.value(ÒfirstNameÓ)=ÓJohnÓ
fields.value(ÒlastNameÓ)=ÓSmithÓ
mysql_update(ÒcustomersÓ,fields,ÓcustomerIDÕ,2)
Of course, you wouldnÕt want to hard-code the values as in
the example above.
There are two ways to insert records into the database. You can do it through an INSERT query
or you can use the mysql_insert
method. When either one is
finished, the mysql_inserted
event will occur.
If you use the mysql_insert method, you must pass to it the
following parameters:
Example:
dim fields as dictionary
fields=new dictionary
fields.value(ÒfirstNameÓ)=ÓJohnÓ
fields.value(ÒlastNameÓ)=ÓDoeÓ
mysql_insert(ÒcustomersÓ,fields)
When the insert has finished, the mysql_inserted event will
have the id of the row inserted (if the table has an auto-increment field) in
the results parameter.
The properties used by this class are:
currentRecord contains all the fields and values of the
current record.
The name of the database to be accessed.
The domain name or IP of the host where the mySQL database
resides.
The URL of where the pseudoMySQL.php script resides.
The number of rows returned from the previous select query.
This is where the current record is located in the records array.
The password that is used to connect to the mySQL database.
An array of dictionaries that contain each row returned from
the previous select query.
The username that is used to connect to the mySQL database.
Since pseudoMySQLÕs super class is HTTPSocket, it is event
driven. Most methods (see below)
have a corresponding event. To
make it easy to remember which events go with which methods, the events have
the same name as the methods with ÒedÓ on the end. For example, the mysql_select methodÕs event is
mysql_selected.
This event occurs after the mysql_connect method has
finished.
This event occurs after the mysql_delete method has finished. The result parameter will contain the
number of rows deleted.
If an error has occurred, errorMessage will contain the
error.
This event occurs after the mysql_execute method has finished.
The result parameter will contain any message returned from the sever.
This event occurs after the mysql_insert method has
finished. The result parameter will contain the id of the row just inserted if
the table has an auto-increment field.
This event occurs after the mysql_update method has
finished.
This event occurs after the mysql_update method has
finished. The result parameter will contain the number of rows affected.
pseudoMySQL has several methods you can use to access a
mySQL database, These methods are:
This method will add a backslash before each tick (Ô), quote
(Ò), and backslash.
Dump will create a text representation of a row or rows
returned from the last select query and put it on the clipboard. This is handy for debugging if you need
to see the fields and values of the result of the query. You can control if you wish to see all
records, the current record, or a specific record. The possible values you may pass are:
The eof method advances to the next row in the record
set. If the last row has been
reached, a true will be returned.
Moves the record set position pointer to the first record.
Moves the record set position pointer to the last record.
This method establishes a connection to the database. Once connected, the mysql_connected
event will occur.
Executes a query.
Once finished, the mysql_executed event will occur.
This method will build and perform an insert query. You pass it the table name and a
dictionary containing the key/value pairs of the data you wish to insert. See Inserting
Data for more information.
When finished, the mysql_inserted
event will occur.
Executes a select query (see Selecting
Data). Once finished, the mysql_selected event will occur.
See Updating Data for a
complete discussion and example of this method. When finished, the mysql_updated event will occur.
See the example project for some examples on how to use this
class.
IÕll be more than happy to answer any questions, receive any
ideas, or hear about any problems you may have. Send an email to macmage@maccrafters.com.