Google
 
 
Home arrow Perl Articles arrow Use Perl DBI For Database Access

Main Menu
 Home
 Linux Articles
 FreeBSD Articles
 Apache Articles
 Perl Articles
 Other Articles
 Program Downloads
 Free Books
 News
 The Web Links
 Contact Us

Most Read
Automating SFTP using expect
FreeBSD PPTP VPN
Shorewall Router on Linux
SnortShorwall - Using Snort And Shorewall Together
Shorewall Stand Alone Firewall

Polls
Favorite Linux/BSD
Fedora
Mandrake
Debian
Slackware
Gentoo
Suse
FreeBSD
Other
  

Syndicate
Latest news direct to your desktop
RSS

Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one

Members Online
 Linux-BSD-Central Has a Total of 701 Members   Members (701) # Online
 We have 6 Guests Online. Guests 6
 We have 0 Users Online. Users 0

Online Users
No Users Online

Statistics
OS: Linux w
PHP: 5.2.9
MySQL: 5.0.91-community
Time: 14:40
Members: 701
Hits: 1324414
News: 277
WebLinks: 15



-->

Use Perl DBI For Database Access   PDF  Print  E-mail 
Contributed by Chad Brandt  
Saturday, 03 July 2004
For many perl programs I write I need to access a relational database. Perl provides the DBI library to access many databases.

This is an example program that shows how you can access MySql and Postgresql from within a perl program

#!/usr/bin/perl
############################################################
# This is an example of connecting to a MySQL database
# and a postgresql database using perl DBI
#
# This requires the DBI:mysql and DBI:Pg modules
#
############################################################
use
DBI;
use
strict;

my
@row;
my
$dbh;
my
$sth;


# This is a mysql example displaying all rows from the user table
$dbh
= DBI->connect('DBI:mysql:mysql:localhost', 'root', 'password');
$sth
= $dbh->prepare(qq{ select * from user});
$sth->execute();
while(@row
= $sth->fetchrow_array()){
    print "@rown";
}
# close the connection
$dbh->disconnect();



# This is a postgres example displaying all rows from the user table
$dbh
= DBI->connect("dbi:Pg:dbname=test;port=5432;user=postgres;password=mypassword);
$sth
= $dbh->prepare(qq{ select * from user_});
$sth->execute();
while(@row
= $sth->fetchrow_array()){
    print "@rown";
}

$dbh->disconnect();

exit(
0);

Comments
Perfect
Written by Guest on 2008-11-14 07:45:01
This was simple and to the point

Write Comment
Name:Guest
Title:
BBCode:Web AddressEmail AddressBold TextItalic TextUnderlined TextQuoteCodeOpen ListList ItemClose List
Comment:



Powered by AkoComment 1.0 beta 2!




 
Google Ads



 

Check out TwistByte - The best mobile apps available For awesome Android and IPhone applications!!