Tuesday, March 31, 2009

Borland C++ Builder 6 and MySQL database HowTo

Source:                                                                        
How to
Use and access a MySQL database with a Borland C++ Builder 6 application
(A simple table and a table with BLOBs)


If you got drivers/DLL problems, please visit the site : http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html

Using Borland dBexpress for Borland Developer Studio 2006

Just take the libmySQL.dll from the MySQL directory to Windows directory.
Go in c:\Program Files\Borland\BDS\4.0\dbExpress\dbxdrivers.ini , verify that
GetDriverFunc=getSQLDriverMYSQL
LibraryName=dbxmys30.dll
VendorLib=libmysql.dll
HostName=localhost
Database=test
User_Name=root
Password=
BlobSize=-1
username and/orpassword depends on your system
Download the BDS examples.
There are just some slight differences, that I corrected to make the old dbexpress versions work fine with BDS2006.




Download :
mydbexpress.zip   BDS2006 : First example, access to MySQL database, browse table elements
mydbexpressblob.zip   BDS2006 : Second example, use BLOB





Using Borland dBexpress

It is for me much simpler, since you need no extra components (dBexpress is inclued in BCB 6 pro).
You need :
  • Borland C++ Builder 6 Pro or Entreprise.
  • MySQL installed (I use Apache 2 (apache_2.0.55-win32-x86-no_ssl.msi), MySQL 4.0 (mysql-4.0.23-win.zip, be aware, MySQL 4.1.15 doesn't work with BCB6 dBExpress) on a WXP PC).
  • My example, see below.
Then you will download the examples and follow the steps :
  • Generate the database tables :
    These tables are in the test database. You have to use phpMyadmin or so to load the sql script and execute it.
    It will create the tables that we will use in our example.
    Don't forget to convert the path of the Blob destination files to point to your directory, example in :
    INSERT INTO ex_2_blob VALUES (1, 'graph', LOAD_FILE('C:\\users\\baghli\\c\\builder\\MySQL\\MyExampleSQL\\graph.txt'), LOAD_FILE('C:\\users\\baghli\\c\\builder\\MySQL\\MyExampleSQL\\graph.bmp'));
  • Copy the files dbexpmysql.dll, dbxconnections.ini, dbxdrivers.ini, libmySQL.dll to your Windows directory.
  • Open the example. You will see database dbExpress VCL components icons : click on the SQLConnection1.
  • SQLConnection1 Params (TSring) value must be :
    • DriverName=MySQLCon, or what ever that is linked to MySQL
    • BlobSize=-1
    • Database=test
    • ErrorResourceFile=
    • HostName=localhost
    • LocaleCode=0000
    • Password=
    • User_Name=root
    Of course, it depends on your local MySQL configuration...

  • Then toggles Connect on true. If everything is ok (nothing seems happening), it has connected, otherwise, you receive an error that tells you that it was imposible to connect or... so check your MySQL and/or dBexpress configuration.
  • Then look to the TableName property of the second VCL component SQLTable1.
    It should points vers table ex_2_blob.

Compile and execute the code. If everything is ok ;-) you will see your appli :
  • Press Open.

  • It will load the DBGrid with the datas (you will see datas.

  • You can navigate through the DBGrid thanks to the DBNavigator.

  • Have fun and make this example more usefull !






Download :
mydbexprexemple.zip   First example, access to MySQL database, through dBexpress, browse table elements with BLOB






Using Zeos VCL components

You need :
  • Borland C++ Builder 6 Pro or Entreprise.
  • MySQL installed (I use Apache 2 and MySQL 4-nt on a W2000 pro PC).
  • Zeos VCL components (free and open source) use the 6.1.5 which is stable + its patch.
  • My examples, see below.
Then you will download the examples and follow the steps :
  • Generate the database tables :
    These tables are in the test database. You have to use phpMyadmin or so to load the sql script and execute it.
    It will create the tables that we will use in our example.
    Don't forget to convert the path of the Blob destination files to point to your directory, example in :
    INSERT INTO ex_2_blob VALUES (1, 'graph', LOAD_FILE('C:\\users\\baghli\\c\\builder\\MySQL\\MyExampleSQL\\graph.txt'), LOAD_FILE('C:\\users\\baghli\\c\\builder\\MySQL\\MyExampleSQL\\graph.bmp'));

  • Open the first example. You will see 3 database VCL components icons : a TZConnection, TZQuery (Z as Zeos !) and TDataSource.
    The DataSource will use the ZQuery1 VCL instance of TZQuery to give info to other db controls (TDBGrid, TDBNavigator)

  • ZQuery1->Connection=ZConnection1 (VCL instance of our TZConnection).

  • ZConnection1 Properties default value must be :
    • Database=test
    • Hostname=localhost
    • Port=3306
    • User=root
    • Password=
    Of course, it depends of your installed MySQL configuration...

  • Then look to the Params (TSring) of ZQuery1.
    It contains :
    SELECT * FROM ex_1 WHERE 1   which is the SQL query we will use to open and see what is in the table ex_1.

Compile and execute the code. If everything is ok ;-) you will see your appli :
  • Press Connect and Open.

  • It will load the DBGrid with the datas (you will se birthdays and...).

  • You can also print the result of the SQL query action in the Memo using the button Print Q result.

  • You can navigate through the DBGrid thanks to the DBNavigator.

ex_1.jpg

Example 2 is similar but it contains Blobs :
Large amount of data like bitmaps and memo text.
Compile and execute the code. Have fun
ex_2.jpg




Download :
myexample1.zip   First example, access to MySQL database, browse table elements
myexample2.zip   Second example, use BLOB
myexamplesql.zip   Initialise database tables for examples 1 and 2



Coba Yahoo! Messenger 9.0 baru
Lengkap dengan segala yang Anda sukai tentang Messenger!

0 Comments: