Delphi 7 connecting to MySql 5.5.15

Source: Internet
Author: User
Tags mysql odbc driver

Delphi 7 connecting to MySql 5.5.15

1. ODBC Mode
You must first have the MySql ODBC driver. I downloaded the fully-installed version, including the MySql server, various supported connections, workbench, sample database, and documentation. If there is no ODBC driver, go to http://dev.mysql.com/downloads/connector/odbc/to download the package.
① Data Source Method
First, confirm that the ODBC Driver of MySql is installed. Open "Control Panel"> "Administrative Tools"> "Data Source", switch to the "drivers" page, and view "MySql ODBC 5.1 Driver ", as shown in:

Switch to "system DSN" → "add" → "MySql ODBC 5.1 Driver", as shown in:

The MySql data source configuration is displayed. The data source name is random. The TCP/IP Server is the Database address, User username, Password, and Database connection. Click "Test" to Test the connection, as shown in:

Click "OK". A new data source is added. Open Delphi 7, place TADOConnection, TADOQuery, TDataSource, and TDBGrid on the form, and associate with each other to configure the connection string of TADOConnection, "Provider" select "Microsoft ole db Provider for ODBC Drivers", next, select "Use Data Source Name", and select the data source you Just configured from the drop-down list. You do not need to fill in other data sources to test the connection, as shown in:


Write a query statement in TADOQuery and set "Active" to "True". The data is displayed in the table, as shown in:
 
② Drive mode
As above, first confirm to install the ODBC driver of MySql. Open Delphi 7, place TADOConnection, TADOQuery, TDataSource, and TDBGrid on the form, and associate with each other. Configure the connection string of TADOConnection as follows:
1
Driver = {MySQL ODBC 5.1 Driver}; Server = 127.0.0.1; Database = world; User = root; Password = a123; Option = 3;
Write a query statement in TADOQuery and set "Active" to "True". The data is displayed in 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 unzip it to the folder. Open Delphi 7 and load the project package "... \ ZEOSDBO \ packages \ delphi7 \ ZeosDbo. bpg ", Compile in sequence, or right-click and select" Compile All From Here ", as shown in:

After compilation, select "ZComponentDesign. bpl" and Right-click "Install". The component is successfully installed. The following dialog box is displayed:

Then add the compiled directory, that is "... \ ZEOSDBO \ packages \ delphi7 \ build ", menu" Tools "→" Environment Options "→" Library "→" Library path ", add this directory. In addition, if you need to enter the ZeosLib source code when writing 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 files will be compiled again under these directories ).
Create an 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
// --------------- Associate with each other ------------------------
Zqry1.Connection: = zcon1;
Ds1.DataSet: = zqry1;
Dbgrd1.DataSource: = ds1;
// --------------- Set the parameter ------------------------
Zcon1.Protocol: = 'mysql ';
Zcon1.Port: = 3306;
Zcon1.HostName: = '2017. 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, you can also manually set it on the designer. Because the MySql database is connected here, you need to copy the DLL (libmysql. dll, which is installed in "... \ MySQL Server 5.5 \ lib ") to the Windows System directory (usually" C: \ WINDOWS \ system32 ") or project directory. Shows the program:
 
When the program is sent to others for use, it must be sent 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, 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 attribute of 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, and DAC for MySQL.

Extended materials:
1. MySQL with Delphi http://delphi.about.com/od/mysql/MySQL_with_Delphi.htm
2. Zeoslib Portal: Home http://zeos.firmos.at/
3. Use ZeosDbo components with MySQL connector and create a Master/Detail data table http://cdwalkman.my-php.net/_tech/mysql_ZeosLib_delphi.htm

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.