Delphi7 connection mysql5.15 Delphi 7 connection 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 ", and add this directory from the menu" Tools "→" Environment Options "→" Library "→" Library path. 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