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
SnortShorwall - Using Snort And Shorewall Together
Shorewall Router on Linux
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 14090 Members   Members (14090) # Online
 We have 12 Guests Online. Guests 12
 We have 0 Users Online. Users 0

Online Users
No Users Online

Statistics
OS: Linux w
PHP: 5.2.17
MySQL: 5.1.56
Time: 11:06
Members: 14090
Hits: 1790109
News: 281
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

Only registered users can write comments.
Please login or register.

Powered by AkoComment 1.0 beta 2!




 


 

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