Go Delphi 7 Connection Mysql/mariadb

Source: Internet
Author: User
Tags mysql odbc driver

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:


  1.  Procedure Tform1.formcreate (Sender:tobject);

  2. Begin

  3. ---------------relate to each other------------------------

  4. Zqry1. Connection: = Zcon1;

  5. DS1. DataSet: = Zqry1;

  6. Dbgrd1. DataSource: = DS1;

  7. ---------------Setting Parameters------------------------

  8. Zcon1. Protocol: = ' mysql ';

  9. Zcon1. Port: = 3306;

  10. Zcon1. HostName: = ' 127.0.0.1 ';

  11. Zcon1. User: = ' root ';

  12. Zcon1. Password: = ' A123 ';

  13. Zcon1. Database: = ' world ';

  14. Zcon1. Connected: = True;

  15. ---------------Query Display------------------------

  16. Zqry1. Close;

  17. Zqry1. Sql. Text: = ' SELECT * from City ';

  18. Zqry1. Active: = True;

  19. 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:


  1.  Begin

  2. Zqry1. Close;

  3. Zqry1. Sql. Text: = ' set names GBK ';

  4. Zqry1. Execsql;

  5. Zqry1. Sql. Text: = ' select * from City ';

  6. Zqry1. Active: = True;

  7. 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


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.