mysql

From Free Pascal wiki
Revision as of 09:43, 14 September 2012 by BigChimp (talk | contribs) (Small corrections)
Jump to navigationJump to search

English (en) español (es) français (fr) polski (pl)

Overview

You can use Free Pascal/Lazarus to access a MySQL database server. If you are looking for information on the mysql package in FPC, please see mysql#MySQL_package:_the_low_level_units below.

Advantages of MySQL:

  • It is very widely used and available
  • Though older versions had a deserved reputation of not being true RDBMSes, newer versions support ACID properties if properly set up (with the right storage backend)

Disadvantages of MySQL:

  • The MySQL maintainers break binary compatibility between client library versions. This means that an FPC/Lazarus translation needs to be made for each new version, which slows things down.
  • The license may be restrictive to some users (e.g. in commercial deployments).

A lot of Lazarus/FPC users prefer Firebird or PostgreSQL databases for this reason.

MySQL licensing

Before a significant MySQL deployment, don't forget to read its license

SQLDB

In Lazarus 1.0 (and FPC 2.6.0), MySQL 4.0, 4.1, 5.0 and 5.1 client libraries are supported by the SQLdb components. If you have FPC 2.6.1 or higher, MySQL 5.5 is also supported.

Make sure you are using the correct connection component for your client library version. So if you have the client libraries installed for MySQL 4.1 then you have to use TMySQL41Connection component, even if the server is running version 4.0 or 5.0.

Zeos

See ZeosDBO

Pascal Data Objects (PDO)

There is an alternative. Functions introduced with MySQL 4.1 and 5.0 like prepared statements, binding, and stored procedures are supported by database API called Pascal Data Objects, which is inspired by PHP Data Objects. All the code and documentation necessary to use this API is available on sourceforge:

[1]

PDO has added drivers for Firebird 1.5 and 2.0


MySQL package: the low level units

As with all databases, the SQLDB code depends on a lower level mysql specific unit that wraps around the mysql driver library (.so/.dll). Normally, you would use the higher-level SQLDB code as it allows you to code more quickly, easily switch databases etc.

Using this is very easy, all you need to do is compile some units, and use these units in your program. You need to specify the location in the filesystem of the MySQL client Library (libmysqlclient on Linux) when compiling, and that is it.

Provided units and programs

The packages provides 3 units, of which normally only the first is needed:

  • mysql the main mysql unit.
  • mysql<version> (e.g. mysql50) provides access to the specific mysql library for that version. Note: the client library version is unrelated to the version of the server in use - except that you want to make sure these versions are compatible.
  • mysql<version>com contains some internal routines of MySQL (presumably a translation of mysql_com.h); it should normally not be used unless you want access to some internal types.

Example programs can be found in the <fpc>\packages\mysql\examples directory.

Installation

The mysql interface is distributed with the Free Pascal packages, and come with the compiler distribution: Normally no action should be taken to work with MySQL.

In case you want to modify and compile the units yourself, the mysql sources are in the packages directory: packages/mysql

This directory contains the units, a test program and a makefile. cd to the directory and type

make

This should compile the units. If compilation was succesful, you can install with

make install

You can then test the program by running

make test

This will:

  • Run a script to create a table in a database, and fill it with some data. (the mysql program should be in your PATH for this) . By default, the used database is testdb.
  • Run the testprogram testdb
  • Run a shell script again to remove the created table.

You will see a lot of messages on your screen, giving you feedback and results. If something went wrong, make will inform you of this.

Go back to Packages List

See also