Click here to Skip to main content
6,822,613 members and growing! (21,276 online)
Email Password   helpLost your password?
Languages » Perl » General     Intermediate

Performing SQL operations through Perl

By lexxwern

This article explains how to perform operations on your database through Perl using the DBI module.
Perl, Dev
Posted:31 Aug 2002
Views:80,934
Bookmarked:6 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 3.34 Rating: 3.50 out of 5

1
1 vote, 33.3%
2

3
1 vote, 33.3%
4
1 vote, 33.3%
5

Introduction

This article explains on how to perform operations on your database through Perl, using the DBI module. This assumes that you have basic knowledge about Perl/CGI and SQL. We will be making a simple table and performing basic SQL operations on it.

Comments

Like all Perl code, this code too is self explanatory. If you need detailed information, don't hesitate to use the article forums.

Example one

Creating a table.

#!/usr/local/bin/perl
use DBI;

$username = '';$password = '';$database = '';$hostname = '';
$dbh = DBI->connect("dbi:mysql:database=$database;" . 
   "host=$hostname;port=3306", $username, $password);

$SQL= "create table user(ID integer primary key " . 
  "auto_increment, username text not null," . 
  " password text not null, email text not null)";

$CreateTable = $dbh->do($SQL);

print "Content-type:text/html\n\n\n";
if($CreateTable){
print "Success";
}
else{
print "Failure
$DBI::errstr"; }

Example two

Inserting a record.

#!/usr/local/bin/perl
use DBI;

$username = '';$password = '';$database = '';$hostname = '';
$dbh = DBI->connect("dbi:mysql:database=$database;" . 
  "host=$hostname;port=3306", $username, $password);

$SQL= "insert into user (username, password, email)" .
  " values('lexxwern', 'password', 'email@host')";

$InsertRecord = $dbh->do($SQL);

print "Content-type:text/html\n\n\n";
if($InsertRecord){
print "Success";
}
else{
print "Failure
$DBI::errstr"; }

Example three

Updating a record.

#!/usr/local/bin/perl
use DBI;

$username = '';$password = '';$database = '';$hostname = '';
$dbh = DBI->connect("dbi:mysql:database=$database;" .
  "host=$hostname;port=3306", $username, $password);

$SQL= "update user set email = ".
  "'lexxwern@yahoo.com' where username = 'lexxwern'";

$UpdateRecord = $dbh->do($SQL);

print "Content-type:text/html\n\n\n";
if($UpdateRecord){
print "Success";
}
else{
print "Failure
$DBI::errstr"; }

Example four

Deleting a record.

#!/usr/local/bin/perl
use DBI;

$username = '';$password = '';$database = '';$hostname = '';
$dbh = DBI->connect("dbi:mysql:database=$database;" .
  "host=$hostname;port=3306", $username, $password);

$SQL= "delete from user where ID=1";

$DeleteRecord = $dbh->do($SQL);

print "Content-type:text/html\n\n\n";
if($DeleteRecord){
print "Success";
}
else{
print "Failure
$DBI::errstr"; }

Example five

Viewing all records.

#!/usr/local/bin/perl
print "Content-type:text/html\n\n";

use DBI;

$username = '';$password = '';$database = '';$hostname = '';
$dbh = DBI->connect("dbi:mysql:database=$database;" .
 "host=$hostname;port=3306", $username, $password);

$SQL= "select * from user";

$Select = $dbh->prepare($SQL);
$Select->execute();

while($Row=$Select->fetchrow_hashref)
{
  print "$Row->{username}
$Row->{email}"; }

Conclusion

Hopefully these examples can give you a neat preview of the capabilities of the DBI module. This site will be of further help. Good luck!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

lexxwern


Member
About
:: a fulltime college student from new delhi, india.

Latest
:: college 1st semester.
Occupation: Web Developer
Location: India India

Other popular Perl articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralConnection to Oracle DB PinsussAnonymous20:55 10 Mar '04  
GeneralJust curious PinmemberMatthew Merritt20:10 13 Dec '03  
GeneralA couple of nitpicks! PinsussAnonymous3:20 10 Dec '02  
GeneralRe: A couple of nitpicks! PinsussAnonymous6:59 9 Mar '03  
GeneralRe: A couple of nitpicks! PinsussAnonymous16:32 19 Mar '03  
GeneralRe: A couple of nitpicks! PinmemberFooOfTheBar2:15 4 Aug '03  
GeneralRe: A couple of nitpicks! PinsussAnonymous8:28 28 Aug '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 31 Aug 2002
Editor: Smitha Vijayan
Copyright 2002 by lexxwern
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project