Solved

Installing the DBI Module

Posted on 2002-05-10
6
126 Views
Last Modified: 2013-12-25
How to?
0
Comment
Question by:lexxwern
  • 3
  • 3
6 Comments
 
LVL 6

Accepted Solution

by:
andreif earned 100 total points
ID: 7002341
perl -MCPAN -e"install DBI"
0
 
LVL 12

Author Comment

by:lexxwern
ID: 7003069
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
 
LVL 6

Expert Comment

by:andreif
ID: 7003560
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 12

Author Comment

by:lexxwern
ID: 7004019
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
 
LVL 6

Expert Comment

by:andreif
ID: 7005331
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
 
LVL 12

Author Comment

by:lexxwern
ID: 7005334
Thankyou for the accurate help and the good link.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question