Perl MySQL

The Perl - MySQL Libs (libraries) are what allow you to use Perl to access your databases. If you plan on using any scripts that require MySQL database access, you will need to install these.

Installing PerlMySQL

  1. Click on the install icon ( ) next to PerlMySQL under the Database section. A new window will open.
  2. Click on the Terms and Conditions link at the bottom of the new window. Another new window will open.
  3. Read the terms and, if you agree, click the box ( ) next to I understand and agree with the Terms And Conditions.
  4. Click the Install button.
  5. Click the Finish button.
  6. When PerlMySQL is successfully installed, the screen will tell you the installation was successful. This screen will also give you the link to your PerlMySQL test page. You may wish to bookmark it. Otherwise, you can access this page at any time in the future by clicking on PerlMySQL under the Database section of your Site Manager.
  7. Close the window.

Using PerlMySQL

DBI is a generic interface for many databases. That means that you can write a script that works with many different database engines without change. You need a DataBase Driver (DBD) defined for each database type. For MySQL, this driver is called DBD::mysql. For more information on the Perl5 DBI, please visit the DBI web page and read the documentation at http://dbi.perl.org/.

For more information on Object Oriented Programming (OOP) as defined in Perl5, see the Perl OOP page at http://language.perl.com/info/documentation.html.

NOTE: If you want to use transactions with Perl, you need to have DBD-mysql version 1.2216 or newer. Version 2.1022 or newer is recommended.

If you have the MySQL module installed, you can find information about specific MySQL functionality with one of the following command:

shell> perldoc DBD/mysql
shell> perldoc mysql

Method

Description

connect

Establishes a connection to a database server.

disconnect

Disconnects from the database server.

prepare

Prepares a SQL statement for execution.

execute

Executes prepared statements.

do

Prepares and executes a SQL statement.

quote

Quotes string or BLOB values to be inserted.

fetchrow_array

Fetches the next row as an array of fields.

fetchrow_arrayref

Fetches next row as a reference array of fields.

fetchrow_hashref

Fetches next row as a reference to a hashtable.

fetchall_arrayref

Fetches all data as an array of arrays.

finish

Finishes a statement and lets the system free resources.

rows

Returns the number of rows affected.

data_sources

Returns an array of databases available on localhost.

ChopBlanks

Controls whether fetchrow_* methods trim spaces.

NUM_OF_PARAMS

The number of placeholders in the prepared statement.

NULLABLE

Which columns can be NULL.

trace

Perform tracing for debugging.

MySQL-specific Methods

Method

Description

insertid

The latest AUTO_INCREMENT value.

is_blob

Which columns are BLOB values.

is_key

Which columns are keys.

is_num

Which columns are numeric.

is_pri_key

Which columns are primary keys.

is_not_null

Which columns CANNOT be NULL. See NULLABLE.

length

Maximum possible column sizes.

max_length

Maximum column sizes actually present in result.

NAME

Column names.

NUM_OF_FIELDS

Number of fields returned.

table

Table names in returned set.

type

All column types.

The Perl methods are described in more detail in the following sections. Variables used for method return values have these meanings:

Portable DBI Methods

$dbh = DBI->connect("DBI:mysql:$database", $user, $password);
$dbh = DBI->connect("DBI:mysql:$database:$hostname",
$dbh = DBI-$user, $password);
>connect("DBI:mysql:$database:$hostname:$port",
$user, $password);

If the user name and/or password are undefined, DBI uses the values of the DBI_USER and DBI_PASS environment variables, respectively. If you don't specify a hostname, it defaults to 'localhost.' If you don't specify a port number, it defaults to the default MySQL port (3306). As of Msql-Mysql-modules Version 1.2009, the $data_source value allows certain modifiers: mysql_read_default_file=file_name. Read `file_name' as an option file. For information on option files

This call will read options defined for the [client] group in the option file. If you wanted to do the same thing but use options specified for the [perl] group as well, you could use this:
$dbh = DBI->connect("DBI:mysql:$database"
. ";mysql_read_default_file=$ENV{HOME}/.my.cnf"
. ";mysql_read_default_group=perl",
$user, $password);

NOTE: Generally the 'do' statement is much faster (and is preferable) than prepare/execute for statements that don't contain parameters.

You can also enable DBI tracing by setting the DBI_TRACE environment variable. Setting it to a numeric value is equivalent to calling DBI->(value) . Setting it to a pathname is equivalent to calling DBI->(2,value).

MySQL-Specific Methods

The methods shown here are MySQL-specific and not part of the DBI standard. Several of them are now deprecated: is_blob , is_key , is_num , is_pri_key , is_not_null , length , max_length , and table. Where DBI -standard alternatives exist, they are noted here:

NOTE: is_not_null is deprecated; it is preferable to use the NULLABLE attribute (described above), because that is a DBI standard.

length

You can use the perldoc command to get more information about DBI.

perldoc DBI
perldoc DBI::FAQ
perldoc DBD::mysql

You can also use the pod2man , pod2html , etc., tools to translate to other formats.

You can find the latest DBI information at the DBI web page: http://dbi.perl.org/.

This manual was adapted from the original from the MySQL documentation and can be found in its original form here http://www.mysql.com/doc/en/Perl.html