Delphi 7 connection MySQL 5.5.15

Source: Internet
Author: User
Tags mysql odbc driver

Delphi 7 connection MySQL 5.5.15

1.ODBC mode
Must first have MySQL ODBC driver, I download is full install version, contains MySQLServer, various supported connection, Workbench, demo sample database, document, if no ODBC drive, can go to official website http://dev.mysql.com/ downloads/connector/odbc/download.
① Data Source Mode
First, verify that the ODBC driver for MySQL is installed. Open Control Panel → administrative tools → data sources to switch to the drivers page. View "MySql ODBC 5.1 Driver", for example, as seen in:

Switch to "System DSN" → "Add" → "MySql ODBC 5.1 Driver", for example as seen in:

The MySQL data source configuration pops up, the data source name is arbitrary, and TCP/IP server is the database address. Userusername,passwordpassword,database the connected database. Click "Test" to test the connection. For example, as seen in:

After clicking "OK", you can see that a new data source has been added. Open Delphi 7 and Place Tadoconnection, Tadoquery, Tdatasource, and Tdbgrid on the window, associating with each other. Configures the connection string for tadoconnection. Provider SelectMicrosoft OLE DB Provider for ODBC Drivers, Next, select Use data source name. Drop-down Select the data source that you just configured, and the others do not need to be filled out, test the connection, for example to see:


writes a query statement in Tadoquery. Set Active to True and the data is displayed on the table, for example, as seen in:

② Drive Mode
As above, first confirm the ODBC driver for MySQL installation. Open Delphi 7, Place Tadoconnection, Tadoquery, Tdatasource and Tdbgrid on the window, 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, for example, as seen 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 number from http://sourceforge.net/projects/zeoslib/files/and extract it to the directory.

Open Delphi 7, load the project package "... \zeosdbo\packages\delphi7\zeosdbo.bpg", compile sequentially, or right-click on "Compile all from Here", for example, as seen in:

After the compilation is complete. Then select "zcomponentdesign.bpl", right-click "Install", the installation component is successful. Popup such as the following dialog box:

Then add the compiled folder. That is, "... \zeosdbo\packages\delphi7\build", menu "Tools" → "Environment Options" → "Library" → "Library path", Join this folder. Also, if you need to write code. Can enter the Zeoslib source code, you need to add the source folder folder. Contains: "... \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 folders).


Create a new application, place tzconnection, Tzquery, Tdatasource, and Tdbgrid on the window, and write the following code in the window creation function:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
procedureTform1.formcreate (Sender:tobject);
begin
//---------------interrelated------------------------
Zqry1. Connection: = Zcon1;
DS1. DataSet: = Zqry1;
Dbgrd1. DataSource: = DS1;
//---------------Set the number of 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 the manual setting on the designer is also possible.

Since the MySQL database is connected here, it is necessary to copy the DLLs required by the MySQL database client (ie:libmysql.dll. Here I install in the "... \mysql\mysql Server 5.5\lib") copy to the WINDOWS system folder (typically "C:\WINDOWS\system32") or under the project folder.

Program for example with what you see:

When the program is sent to others, it is required to send it together with "Libmysql.dll". If the displayed data is garbled, this is because the MySQL database uses the UTF-8 character set by default. Set the character set before the query, for example, as seen 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;&NBSP
end ;  

or add "codepage=gbk" to the Properties property of the Tzconnection.

3. Other methods

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.