Delphi 7 connection MySQL 5.5.15

Source: Internet
Author: User
Tags mysql odbc driver

 Delphi 7 connection MySQL 5.5.15Category: 5.2 Delphi Programming 2011-09-26 17:12 12156 people read reviews (0) favorite reports Delphimysql database Microsoftdatabasewindows Online There are many articles about Delphi connection MySQL database, here, I only record the method I have tested, in case of need.
System environment: Windows XP SP3
Software Environment: Delphi 7, mysql-installer-5.5.15.0

1.ODBC mode
Must first have MySQL ODBC driver, I download is full install version, including MySQL server, various supported connection, Workbench, sample database, document, if no ODBC drive, can go to official website http://dev.mysql.com/downloads/ connector/odbc/download.
① Data Source Mode
First, confirm that you have installed MySQL ODBC driver, open "control Panel" → "Administrative Tools" → "data source", switch to "driver" page and view "MySQL ODBC 5.1 Driver" as shown in:

Switch to "System DSN" → "Add" → "MySql ODBC 5.1 Driver" as shown in:

Pop-up MySQL data source configuration, data source name arbitrarily, TCP/IP server for database address, user username, password password, database connection, point "test" to test the connection, as shown in:

When you click "OK", you will see a new data source added. Open Delphi 7, Place Tadoconnection, Tadoquery, Tdatasource, and Tdbgrid on the form, associate with each other, configure the Tadoconnection connection string, "provider" select "Microsoft OLE DB Provider for ODBC Drivers ", Next, select" Use data source name ", drop-down to select the data source that you just configured, other than fill out, test the connection as shown:

In the Tadoquery write query statement, set active to True, the data is displayed on the table as shown in:

② Drive Mode
As above, first confirm the ODBC driver for MySQL installation. Open Delphi 7, Place Tadoconnection, Tadoquery, Tdatasource, and Tdbgrid on the form, associate with each other, configure the Tadoconnection connection string for the following:
1 Driver={mysql ODBC 5.1 Driver}; server=127.0.0.1; Database=world; User=root; password=a123; option=3;
In the Tadoquery write query statement, set active to True, the data is displayed on the table as shown in:

2.ZeosLib mode
Zeoslib is a database middleware for Borland development tools, including Delphi, C + + builder, and Kylix. Download the "zeosdbo-6.6.6-stable" version from http://sourceforge.net/projects/zeoslib/files/and extract it to the folder. Open Delphi 7, load the project package "... \zeosdbo\packages\delphi7\zeosdbo.bpg", compile sequentially, or right-click on "Compile all from Here" as shown in:

After compiling, select "ZCOMPONENTDESIGN.BPL", right click "Install", the installation component succeeds, the following dialog box appears:

Add this directory by adding the compiled completed directory, "... \zeosdbo\packages\delphi7\build", the menu "Tools" → "Environment Options" → "Library" → "Library path". In addition, if you need to write code, you can enter the Zeoslib source code, you need to add the source code folder directory, including: "... \zeosdbo\src\core", "... \zeosdbo\src\parsesql", "... \zeosdbo\ Src\plain "," ... \zeosdbo\src\dbc "and" ... \zeosdbo\src\component "(Note: The unit file will be compiled again under these directories).
Create a new application, place tzconnection, Tzquery, Tdatasource, and Tdbgrid on the form, and write the following code in the form creation function:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
Procedure Tform1.formcreate (Sender:tobject);
Begin
---------------relate to each other------------------------
Zqry1. Connection: = Zcon1;
DS1. DataSet: = Zqry1;
Dbgrd1. DataSource: = DS1;
---------------Setting Parameters------------------------
Zcon1. Protocol: = ' mysql ';
Zcon1. Port: = 3306;
Zcon1. HostName: = ' 127.0.0.1 ';
Zcon1. User: = ' root ';
Zcon1. Password: = ' A123 ';
Zcon1. Database: = ' world ';
Zcon1. Connected: = True;
---------------Query Display------------------------
Zqry1. Close;
Zqry1. Sql. Text: = ' SELECT * from City ';
Zqry1. Active: = True;
End
Of course, it is also possible to manually set the settings on the designer. Because the MySQL database is connected here, you need to copy the DLLs required by the MySQL database client (ie: Libmysql.dll, which I installed in the ". \mysql\mysql Server 5.5\lib") copied to the Windows system directory ( Usually "C:\WINDOWS\system32") or under the engineering directory. The program looks like this:

When the program is sent to someone else, it needs to be delivered together with "Libmysql.dll". If the displayed data is garbled because the MySQL database uses the UTF-8 character set by default, set the character set before the query, as shown in the following code:
1
2
3
4
5
6
7
Begin
Zqry1. Close;
Zqry1. Sql. Text: = ' set names GBK ';
Zqry1. Execsql;
Zqry1. Sql. Text: = ' select * from City ';
Zqry1. Active: = True;
End

or add "CODEPAGE=GBK" to the Properties property of the Tzconnection.

3. Other ways

In addition, there are other third-party controls that can connect to other databases such as MySQL, such as Anydac, MYDAC, DAC for MySQL, and so on.

Extended Data:
1.MySQL with Delphi http://delphi.about.com/od/mysql/MySQL_with_Delphi.htm
2.Zeoslib Portal:: Home http://zeos.firmos.at/
3. Use the zeosdbo element with MySQL connection and build the Master/detail information sheet http://cdwalkman.my-php.net/_tech/mysql_ZeosLib_delphi.htm

Delphi 7 connection MySQL 5.5.15

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.