(At present this is only a slightly warmed over version of the standard POD style documentation run through pod2html. -mke)
SUMMARY:
use DBI; $dbh = DBI->connect( $host, $database, $user, 'mysql' [, $password] ); # or $dbh = DBI->connect( '', '', '', 'mysql', { hostname => "localhost", port => 3333, database => "test", username => "myname", password => "mypassword", });
EXAMPLE:
#!/usr/bin/perl use DBI; $drh = DBI->install_driver('mysql'); $dbh = $drh->connect('','test','','') or die "Error connecting to database"; $dbh->do("DROP TABLE foo"); $dbh->do("CREATE TABLE foo ( Field_1 VARCHAR(60) )"); $count = 1; while ($count < 500) { $sth = $dbh->prepare("INSERT INTO foo VALUES('XXXYYYZZZ')"); $sth->execute; $sth->finish; # It is VERY important to call finish, otherwise # you will loose the memory. print "$count\n" unless $count % 10; $count = $count + 1; }
$host can be a simple hostname (``db.domain.com'') or a hostname and port (``db.domain.com:3333'').
$database is the name of the database to connect to.
$user is the username to connect as (leave this blank to connect as the username corresponding to the process' effective uid). $password is optional and should only be specified for accounts that have non blank passwords.
The hash keys which DBD::mysql uses are:
hostname
database
username
password
port
You can enable debugging by setting the environment variable
MYSQL_DEBUG
to 'd:t:O,filename'
(where
filename
is the file to log to). This will give a dbug log
in filename if libmysql is compiled with DBUG support. (See the mysql
documentation for more details on DBUG).
The following tags are supported:
TABLE
TYPE
IS_PRI_KEY
IS_NOT_NULL
LENGTH
MAXLENGTH
IS_KEY
IS_BLOB
IS_NUM
To get the value of an AUTO_INCREMENT
field after an INSERT
, use:
$id = $dbh->func("_InsertID");
You can use the following work arounds. First of all, always treat values that may be large as strings. As long as you do this they can be displayed and re-inserted into a database without incident. The same goes for inserting new values into tables. If you set a variable called $tmpvar to be equal to "4147483647" and then INSERT it into your database you should be fine. Note that the ""'s are important here.
If you need to do calculations involving large numbers you should do them via the initial SELECT.
In order to insert binary data into the database you will have to escape certain characters. You may use the following method.
$mystring = $dbh->quote($rawstring);
_InsertID
by Nathan Torkington.