lexxwern
asked on
Installing the DBI Module
How to?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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,$pas swd) 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;
use DBI;
# connect to DB
$dbh = DBI->connect ("DBI:mysql:database=$db",
# 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.
$sth->execute("test2@email
$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;
ASKER
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 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
$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
ASKER
Thankyou for the accurate help and the good link.
ASKER
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...