Team LiB
Previous Section Next Section

Writing Scripts

Every Perl script that uses the DBI module must include the following line:

use DBI;

It's normally not necessary to include a use line for a particular DBD-level module because DBI will take care of activating the proper module when you connect to the server.

Normally, a DBI script opens a connection to a MySQL server using the connect() method and closes the connection with disconnect(). While the connection is open, SQL statements may be issued. The methods used to execute statements vary depending on the type of statement. Non-SELECT statements typically are performed with the do() method. SELECT statements typically are performed by passing the statement to prepare(), and then calling execute(), and finally retrieving the result set a row at a time in a loop that repeatedly invokes a row-fetching method, such as fetchrow_array() or fetchrow_hashref().

When you issue statements from within a DBI script, each statement string must consist of a single SQL statement, and should not end with a semicolon character (';') or a \g sequence. ';' and \g are conventions of the mysql client program and are not used for DBI.

    Team LiB
    Previous Section Next Section