[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 134
  • Last Modified:

Installing the DBI Module

How to?
0
lexxwern
Asked:
lexxwern
  • 3
  • 3
1 Solution
 
andreifCommented:
perl -MCPAN -e"install DBI"
0
 
lexxwernAuthor Commented:
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...
0
 
andreifCommented:
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;
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
lexxwernAuthor Commented:
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?
0
 
andreifCommented:
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
0
 
lexxwernAuthor Commented:
Thankyou for the accurate help and the good link.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now