2008/07/25

The Labs.Com Issue_05_Database
Last update 1999/02/20

TPJ: Issue_05_Database

This is a collection of programs published by The Perl Journal. You can download all source-code also from TPJ: Programs.
  1. execute.pl
  2. inout.pl
  3. select.pl
  4. More Samples on Database
Issue_05_Database
1. execute.pl
Download execute.pl

 #!/usr/bin/perl -w 
 # 
 # (c)1996 Alligator Descartes <descarte@hermetica.com> 
 # 
 # execute.pl: Connects to a database called 'test' on a  
 #             given database, then EXECUTEs an update 
 #             statement. This is non-cursorial, so do() 
 #             is used. 
  
 use DBI; 
  
 if ( $#ARGV < 0 ) { 
     die "Usage: execute.pl <Database String> <Database Vendor\ 
 >\n"; 
   } 
  
 # Create new database handle. If we can't connect, die() 
 $dbh = DBI->connect( '', $ARGV[0], '', $ARGV[1] ); 
 if ( !defined $dbh ) { 
     die "Cannot connect to mSQL server: $DBI::errstr\n"; 
   } 
  
 # Prepare the statement for immediate execution 
 $rv = 
     $dbh->do( " 
         UPDATE table 
         SET name = 'Alligator Descartes' 
         WHERE id = 1 
       " ); 
 if ( !defined $rv ) { 
     die "Statement execution failed: $DBI::errstr\n"; 
   } 
 # Disconnect from the database 
 $dbh->disconnect; 
  
 exit; 

Issue_05_Database
2. inout.pl

Download inout.pl

 #!/usr/bin/perl -w 
 # 
 # (c)1996 Alligator Descartes <descarte@hermetica.com> 
 # 
 # inout.pl: Connects and disconnects from a database called 'test' on \ 
 a  
 #           local mSQL server 
  
 use DBI; 
  
 if ( $#ARGV < 0 ) { 
     die "Usage: inout.pl <Database String> <Database Vendor&g\ 
 t;\n"; 
   } 
  
 # Create new database handle. If we can't connect, die() 
 $dbh = DBI->connect( '', $ARGV[0], '', $ARGV[1] ); 
 if ( !defined $dbh ) { 
     die "Cannot connect to mSQL server: $DBI::errstr\n"; 
   } 
  
 # Disconnect from the database 
 $dbh->disconnect; 
 exit; 

Issue_05_Database
3. select.pl

Download select.pl

 #!/usr/bin/perl -w 
 # 
 # (c)1996 Alligator Descartes <descarte@hermetica.com> 
 # 
 # select.pl: Connects to a database called 'test' on a  
 #            given database, then SELECTs some basic data 
 #            out in array and scalar forms 
 use DBI; 
 if ( $#ARGV < 0 ) { 
     die "Usage: inout.pl <Database String> <Database Vendor&g\ 
 t;\n"; 
   } 
 # Create new database handle. If we can't connect, die() 
 $dbh = DBI->connect( '', $ARGV[0], '', $ARGV[1] ); 
 if ( !defined $dbh ) { 
     die "Cannot connect to mSQL server: $DBI::errstr\n"; 
   } 
 # Prepare the statement for execution 
 $sth = 
     $dbh->prepare( " 
         SELECT id, name 
         FROM table 
       " ); 
 if ( !defined $sth ) { 
     die "Cannot prepare statement: $DBI::errstr\n"; 
   } 
 # Execute the statement at the database level 
 $sth->execute; 
  
 # Fetch the rows back from the SELECT statement 
 while ( @row = $sth->fetchrow() ) { 
     print "Row returned: @row\n"; 
   } 
 # Re-execute the statement to bring the rows back again 
 $sth->execute; 
  
 # Fetch the data back into separate variables this time 
 while ( ( $id, $name ) = $sth->fetchrow() ) { 
     print "ID: $id\tName: $name\n"; 
   } 
 # Release the statement handle resources 
 $sth->finish; 
  
 # Disconnect from the database 
 $dbh->disconnect; 
 exit; 

Issue_05_Database
4. More Samples on Database

  • Issue_05_Database

                                                                                                                                   

Last update 1999/02/20

All Rights Reserved - (C) 1997 - 2008 by The Labs.Com

Top of Page

The Labs.Com