DESY HOME IT HOME Print Preview Print Window

Perl DBI at DESY

The DBI is the standard database interface for the Perl programming language. The DBI is database independent, which means that it can work with just about any database.
In principle the DBI allows you to connect to different databases at the same time. The benefit is, you can move data from one database to another or you can get information from different databases at the same time. All you have to do is, build a connection to all relevant databases.
Run this script for reporting the available drivers.

#!/opt/products/bin//perl
# This path may be different, it is platform dependent.
#
use DBI;
my @drivers = DBI->available_drivers;
print "Available drivers are:\n\t", join("\n\t", @drivers), "\n";
exit;

If the needed driver is missing, contact the UCO or your system administator.

Below you will find a sample file which you may run to connect to the ORACLE database instance DESY.
In this sample script are two important handles. The first one $dbh is the database handle which handles the connection. The second $sth is the statement handle, it actually encapsulates individual SQL statements to be executed within the database.

If the operating system is UNIX, don't forget to set the environment.
If the operating system is WINDOWS several network components have to be installed, some dynamic load libraries (dll) must be available and environment settings have to be done.
Simply install SQL/ODBC by using NETINSTALL and all needed dlls are on your computer and the needed environment settings are done.
To check the environment and applications based on ORACLE using SQL*PLUS will be helpfull.

Sample

Copy this sample file to sample.pl and run it. The result will be a listing of the employee number and name which is stored in the table emp of the test account scott. This script can be used to execute other queries. Simply insert your account and password into $dbuser, $pass, change the table name from emp to your table name and insert the column names.
If WINDOWS is your operating system, call the DOS command prompt and execute perl sample.pl


#!/opt/products/bin//perl
# This path may be different, it is platform dependent.
#

use DBI;
#---------------------------------------------------------------------------
#-- change user name, password, table name, column names
#---------------------------------------------------------------------------
$dbname = 'dbi:Oracle:desy';
$dbuser = 'scott';
$pass = 'tiger';
$table = 'emp';
$columns = 'empno,ename';
#
#---------------------------------------------------------------------------
#
# connect to the database
$dbh = DBI->connect($dbname, $dbuser, $pass) or die "Cant connect to : $DBI::errstr\n";
# select entries in the database
$sth = $dbh->prepare(qq{select $columns from $table});
# execute the select statement
$sth->execute;
while (@row = $sth->fetchrow_array)
{
print "@row\n";
}
# end the reading of results
$sth->finish;
# disconnect from the database
$dbh->disconnect;
# that's all
exit;

Imprint © 2010 Last update: 07. Feb. 2006 www-it@desy.de