Link to home
Start Free TrialLog in
Avatar of lexxwern
lexxwernFlag for Netherlands

asked on

Installing the DBI Module

How to?
ASKER CERTIFIED SOLUTION
Avatar of andreif
andreif
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lexxwern

ASKER

ok i did install it, about.com, but anyway thanks to andreif for effort too.

anyway now a little change in the Q;

can im looking for a simple example, where tables are created and values written and read.

thanks...
it depends on database, this example is for MySQL and queries can be not working with other databases. Anyway DBI module interface is the same.

use DBI;

# connect to DB
$dbh = DBI->connect ("DBI:mysql:database=$db",$user,$passwd) or die $DBI::errstr;

# create table
$sql = "
CREATE TABLE blocked_emails (
  email varchar(100)
)
";
$dbh->do($sql) or print $DBI::errstr;

# insert records
$sql = "insert into blocked_emails (email) values (?)";
$sth = $dbh->prepare($sql) or print $DBI::errstr;

$sth->execute("test@email.com") or print $DBI::errstr;
$sth->execute("test2@email.com") or print $DBI::errstr;

$sth->finish;


# select records
$sql = "select * from blocked_emails";
$sth = $dbh->prepare($sql) or print $DBI::errstr;
while ($record = $sth->fetchrow_hashref) {
  print "$$record{email}<br>";
}
$sth->finish;
thanks andreif,

yes i use mysql, sorry for not letting you know earlier.

your examples were quite useful. a small doubt.


suppose a select returns more than one value, then how can it be written into arrays in one go.... or will i have to get data rowwise?
Yes, you can use "fetchall_arrayref"

$tbl_ary_ref = $sth->fetchall_arrayref;

The fetchall_arrayref method can be used to fetch all the data to be returned from a prepared and executed statement handle. It returns a reference to an array that contains one reference per row.

This link can be useful for you:

http://www.mysql.com/doc/P/e/Perl_DBI_Class.html
Thankyou for the accurate help and the good link.