connection.open

Description

The open method procedure of the connection object type is used to open a connection to a database engine.

Example

For example, the simple program below illustrates how to call the open method. NOTE: The actual connection strings, you will use in your own programs will probably differ from the one used in the program below.

program Connect;
var
   conn : connection;
begin
   new(conn); //Create instance of connection object before using it
   if supported(feature_odbc) then
      conn.open('ODBC;DSN=test;user=sa;password=')
   else if supported(feature_mysql) then
      conn.open('MYSQL;user="sa";password="";socket="/tmp/mysql.soc"')
   //
   //Add code here to process database
   //
   conn.close;
   dispose(conn); //Destroy instance of connection object when finished
end.

Parameter

The only parameter to this procedure is the connection string, which is an expression of type string.

NOTE: Before an attempt is made to actually open a connection, the connection string is processed to determine what kind of connection to open and what are the parameters to use for the connection. The connection string is processes as follows:

The connection string must be prefixed by a connection type specifier, which is separated from the actual connection string by a semi-colon (;).

First the connection type specifier is separated from the connection string. Next the connection type specifier is compared with ODBC, in such a way that case is not significant, and if there is a match then an ODBC connection is to be opened. Since case is not significant in this comparision the connection type specifier could be lowercase (as in odbc), uppercase (as in ODBC), or mixed case. If the connection type specifier does not match ODBC, then it is compared with MYSQL, again in such a way that case is NOT significant. If the connection type specifier matches MYSQL then a MySQL connection is to be opened. It is an error if the connection string is not prefixed by a connection type specifier or if the connection type specifier does not match either ODBC or MYSQL.

Opening ODBC Connections

Next if an ODBC connection is to be opened then the connection string must be in one of the following two forms:

   odbc-connection-string =
       odbc-connection-string-1 |
       odbc-connection-string-2 |

   odbc-connection-string-1 =
        dsn-parm ; uid-parm ; pwd-parm [ ; [ driver-specific-text ] ]

   dsn-parm = DSN = name

   uid-parm = UID = uid

   pwd-parm = PWD = [ password ]

   odbc-connection-string-2 = name ; [uid] ; [ password ]

where [] indicate optional parameters.

For example  DSN=test;UID=sa;PWD=

When the connection string is in the first form then it is passed, without further processing, to the ODBC API function SQLDriverConnect to open the connection.

When the connection string is in the second form then the name, id, and password parameters are extracted from the connection string, if present, and passed to the ODBC API function SQLConnect to open the connection. NOTE: The first form of the connection string is the recommended form, support for the second form is provided for completeness only.

Opening MySQL Connections

If a MySQL connection is to be opened then the connection string must be in the following form:

   mysql-connection-string = 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    user="testuser";database="testdb";socket="/tmp/mysql.soc";

The connection parameters are extracted from the connection string and passed to the MySQL C API function mysql_real_connect to open the connection.

The effect of each of the parameters is described below:

Portability

Operating Systems: All
Standard Pascal: No