Portable DBI methods.
| connect | Establish a connection to a database server |
| prepare | Get a SQL statement ready for execution |
| do | Prepares and executes a SQL statement |
| disconnect | Disconnect from the database server |
| quote | Quote strings/blobs to be inserted |
| execute | Executes prepared statements |
| fetchrow_array | fetch the next row as an array of fields. |
| fetchrow_arrayref | fetch next row as a reference array of fields |
| fetchrow_hashref | fetch next row as a reference to a hashtable |
| fetchall_arrayref | Get all data as a array of arrays |
| finish | finish a statment and let the system free resources |
| rows | Returns the number of rows affected |
| data_sources | Return an array of databases available on localhost |
| ChopBlanks | Shall fetchrow trim spaces |
| NUM_OF_PARAMS | Number of placeholders in the prepared statement |
| NULLABLE | Which columns can be NULL |
| insertid | The latest auto_increment value |
| is_blob | Which column ar BLOBs |
| 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 can NOT be NULL. See NULLABLE |
| length | Maximum theoretically possible column sizes |
| max_length | Maximum physical present column sizes |
| NAME | Column names |
| NUM_OF_FIELDS | Number of fields returned. |
| table | Table names in returned set |
| type | All coulumn types |
| _CreateDB | Create a database |
| _DropDB | Drop a database. THIS IS DANGEROUS |
connect
$data_source value should begin with
DBI:driver_name:.
Example connect methods with the DBD::mysql driver:
$dbh = DBI->connect("DBI:mysql:$database", $user, $password);
$dbh = DBI->connect("DBI:mysql:$database:$hostname",
$user, $password);
$dbh = DBI->connect("DBI:mysql:$database:$hostname:$port",
$user, $password);
If the username and/or password are undefined, then the DBI will use the
values of the DBI_USER, DBI_PASS environment variables
respectively. If you don't specify a hostname, then it will default to
"localhost". If you don't specify a port, then it defaults to the
default mysql port (3306).
prepare
($sth) which invokes the execute
method. Example:
$sth = $dbh->prepare($statement) or die "Can't prepare $statement: $dbh->errstr\n";
do
$rc = $dbh->do($statement) or
die "Can't execute $statement: $dbh- >errstr\n";
disconnect
$rc = $dbh->disconnect;
quote
$sql = $dbh->quote($string)
execute
fetch_* methods below to retrieve the data. Example:
$rv = $sth->execute or die "can't execute the query: $sth->errstr;
fetchrow_array
while(@row = $sth->fetchrow_array) {
print qw($row[0]\t$row[1]\t$row[2]\n);
}
fetchrow_arrayref
while($row_ref = $sth->fetchrow_arrayref) {
print qw($row_ref->[0]\t$row_ref->[1]\t$row_ref->[2]\n);
}
fetchrow_hashref
while($hash_ref = $sth->fetchrow_hashref) {
print qw($hash_ref->{firstname}\t$hash_ref->{lastname}\t\
$hash_ref- > title}\n);
}
fetchall_arrayref
my $table = $sth->fetchall_arrayref or die "$sth->errstr\n";
my($i, $j);
for $i ( 0 .. $#{$table} ) {
for $j ( 0 .. $#{$table->[$i]} ) {
print "$table->[$i][$j]\t";
}
print "\n";
}
finish
$rc = $sth->finish;
rows
$rv = $sth->rows;
NULLABLE
$null_possible = $sth->{NULLABLE};
NUM_OF_FIELDS
$nr_of_fields = $sth->{NUM_OF_FIELDS};
data_sources
@dbs = DBI->data_sources("mysql");
ChopBlanks
$sth->{'ChopBlanks') =1;
insertid
$new_id = $sth->{insertid};
is_blob
$keys = $sth->{is_blob};
is_key
$keys = $sth->{is_key};
is_num
$nums = $sth->{is_num};
is_pri_key
$pri_keys = $sth->{is_pri_key};
is_not_null
$not_nulls = $sth->{is_not_null};
max_length
length
$max_lengts = $sth->{max_length};
$lengts = $sth->{length};
NAME
$names = $sth->{NAME};
table
$tables = $sth->{table};
Go to the first, previous, next, last section, table of contents.