Using MySQL databases with Lazarus/Free Pascal.
MySQL is probably the most used database server on the Web. Beside the commercial editions (standard, enterprise, cluster...) there is a completely free MySQL community edition. For details about MySQL, you may want to have a look at my article, concerning the installation of MySQL as part of a MS Windows web development environment. |
The following tutorial is about using MySQL databases from within a Lazarus/Free Pascal application. It covers the basics, but I think, that's enough to know (at least for beginners) in order to be able to transfer data between the database server and your program. Please, note that the tutorial examples have been build and tested on Windows 10, using Lazarus 1.8.4 64bit (with FPC 3.0.4); the database server used has been MySQL 5.7.22 64bit. Concerning newer versions, some important details are given in the Connecting to MySQL 8 paragraph below. |
The tutorial is divided into 3 parts: |
1. Connecting to a MySQL database |
Tutorial, showing in detail, how to proceed to connect to a MySQL database, using a TMySQLConnection object, or, as an alternative, using a TODBCConnection object. |
2. Reading data from a MySQL database |
Tutorial, showing the very basics of reading (selecting) data from a MySQL database from within a Lazarus application. It also contains the information of what to do, to correctly handle UTF-8 characters. |
3. Writing data to a MySQL database |
Tutorial, about how to change the actual content of a MySQL database. The tutorial shows a simple Lazarus application, to insert, delete and update database records. You also find there, how you can proceed, to check if your queries were executed successfully or failed. |
Note: Connecting to MySQL 8. |
The tutorial examples should work with other versions than Windows 10 64bit, as well as with other (more recent) Lazarus and MySQL versions, than those, I used when writing the tutorial. Just, be aware, that there may be problems with the client DLL, as explained in part 1 of the tutorial; in the case, where you don't succeed using the TMySQLxxConnection, the TODBCConnection should always work. |
If you experience problems with your installation and for whatever reason you don't want to use ODBC, the description of my experiences with MySQL 8 may be helpful. |
Here my installation from January 2021:
|
And here my actual (April 2023) installation:
|
UTF-8 access to MySQL 8.0 seems to be different from earlier versions. Not only that the 2 queries described in the Reading data from a MySQL database tutorial, are no longer needed, but on the contrary, non-ANSI characters are handled correctly by default and appear as garbage, if these queries are actually executed! |
The MySQL 8 "world" database (database used in the tutorial), seems to be badly configured. With Latin character set tables, accentuated characters appeared as "garbage" in my tables. Drop the database and recreate it manually, using CHARSET=utf8mb4 (the server default), then insert the countries, cities and languages records. You can use the following link to download the SQL of the "world" database. If you use the SQL in this file, a foreign key index, relating the "city" and the "country" tables, will be created, thus no need to do it manually, as described in the third part of the tutorial. |
If you find these tutorials helpful, please, support me and this website by signing my guestbook.