Known as one of the most popular open source database, MySQL is widely used in various scenarios, ApsaraDB RDS for MySQL of Alibaba Cloud provided critical relief for companies, especially small and medium-sized enterprises affected by novel coronavirus (COVID-19).
Source: 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 and Place Tadoconnection, Tadoquery, Tdatasource, and Tdbgrid on the form, associating with each other ( The practice here is to tadoquery the name of the Tadoconnection control on the Connection Property Association, tdatasource the query property to associate the actual name of the control on the Tadoquery, the order of the above, and so on), Configure the connection string for Tadoconnection, "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 is not filled in, Test the connection as shown in:
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:
|
|
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:
-
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:
-
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
Go Delphi 7 Connection Mysql/mariadb