Delphi 7 connection MySQL 5.5.15
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, the other does not have to be filled 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:
When the compilation is complete, selectZCOMPONENTDESIGN.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 |
|
procedureTform1.formcreate (Sender:tobject); begin //---------------interrelated------------------------ Zqry1. Connection: = Zcon1; DS1. DataSet: = Zqry1; Dbgrd1. DataSource: = DS1; //---------------Set 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, where I installed the "... \mysql\mysql Server 5.5\lib") Copy to the WINDOWS system directory (usually "C:\WINDOWS\system32") or the project 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;&NBSP 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