Name

connect

Synopsis

$db_handle = DBI->connect(data_source, user, passwd,[\%attr])

Connects to the specified database, returning a database handle object. The connect method automatically installs the driver if it has not already been installed.

data_source

A string identifying the driver to connect to, and any other additional driver-specific information needed to identify the driver (e.g., a port number). The driver name is written in the form dbi:driver_name, e.g., dbi:mysql. (Default value is taken from the DBI_DSN environment variable.) For example, a connection string for the MySQL driver always starts with dbi:mysql:. The second colon should be followed by any information required by the driver to make the database connection. In the case of MySQL, you must supply a database name (bookdb in the following example), a hostname (localhost), and the port number of the database server (1114):

DBI:$driver:database=$database;host=$hostname;port=
$port
user

The username for the database connection. (Default value is taken from the DBI_USER environment variable.)

passwd

The password for the database connection. (Default value is taken from the DBI_PASS environment variable. Set this value at your own risk.)

\% attr

A hash reference defining attributes for the database handle.

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.