Contents | Prev | Next

2.5.1.2 Connecting to MySQL databases

What is MySQL?

MySQL is a popular open source database management system (DBMS). MySQL is particular popular for computers running Linux and FreeBSD. You can go to www.MySQL.com for more information about MySQL.

Connecting to MySQL databases

In order to connect to a MySQL database, you need to:

  1. Declare a variable of the built-in type connection
  2. Use new on the variable to create a connection object
  3. Invoke the open method of the connection object
The open method of the connection object takes one argument, the connection string. MySQL connection strings take the following form:

mysql-connection-string = 'MYSQL' ';' mysql-parameter-list

mysql-parameter-list = mysql-parameter ';' mysql-parameter-list | empty

mysql-parameter = mysql-host-parameter | mysql-user-parameter | mysql-password-parameter | mysql-database-parameter | mysql-port-parameter | mysql-socket-parameter | mysql-compress-parameter

mysql-host-parameter = 'host' '=' '"' host-name '"'

mysql-user-parameter = 'user' '=' '"' user-name '"'

mysql-password-parameter = 'password' '=' '"' password '"'

mysql-database-parameter = 'database' '=' '"' database-name '"'

mysql-port-parameter = 'port' '=' port-number

mysql-socket-parameter = 'socket' '=' '"' socket '"'

mysql-compress-parameter = 'compress' '=' boolean-value

boolean-value = 'yes' | 'no' | 'true' | 'false'

For example MYSQL;user="testuser";database="testdb";socket="/tmp/mysql.soc";

The connection parameters are extracted from the connection string and passed to mysql_real_connect to open the connection. NOTE: mysql_real_connect is the MySQL C API function that is used to open a connection to a MySQL database.

The effect of each of the parameters is described below:

The mysql-host-parameter specifies the hostname or IP address of the MySQL database server. If mysql-host-parameter is not specified or if host-name is an empty string or is equal to "local-host" then the connection is opened to the local MySQL server over a UNIX socket.

The mysql-user-parameter specifies the username used to connect to the MySQL database server. If the mysql-user-parameter is not specified or if user-name is an empty string then the login name of the person running the application is used.

The mysql-password-parameter specifies the password of the user who will be connected to the database server. If the mysql-password-parameter is not specified or if password is an empty string then the connection is rejected if the user actually has a password.

The mysql-database-parameter specifies the initial database selected when the connection is opened. If the mysql-database-parameter is not specified or if database is an empty string then no initial database is selected. In which case you must call the selectdatabase method later on to select a database.

The mysql-port-parameter specifies the port used to remotely connect to a MySQL database server over TCP. If the mysql-port-parameter is not specified or if port-number is 0 then the default port is used.

The mysql-socket-parameter specifies the filename of the UNIX socket used to connect to a MySQL database server on the local machine. If the mysql-socket-parameter is not specified or if socket is an empty string then the default socket is used.

The mysql-compress-parameter specifies the compression is to be used when communicating with the MySQL database server. If the mysql-compress-parameter is not specified then compression is not used.

See the Irie Pascal Programmer's Reference Manual (in "progref.html") for more information.

Contents | Prev | Next