Solved

Installing the DBI Module

Posted on 2002-05-10
6
124 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now