DB2 Interfaces with Perl This page will provide a sample Perl program that is talking to DB2 using DBI. DBI is an interface between an application and the database drivers. It provides a standard interface and framework for the drivers to operate within. #!/usr/bin/perl -w ################################################# # Telling Perl to use DBI to interface with a DB2 database ################################################# use CGI; MAIN: { use DBI; use DBD::DB2::Constants; use DBD::DB2 qw($attrib_int $attrib_char $attrib_float $attrib_date $attrib_ts); ################################################## # CONNECT TO THE DATABASE # Here you would substitute DATABASENAME with the name of your DB2 # database and then substitute username with a valid username for # accessing the DB2 database. #The open statement will get the password from a user defined file. # A different way to get the password would be to hard code it in the # program. #The $dbh statement is actually establishing the connection to the # database ################################################## $c_dbname = join '','dbi:DB2:DATABASENAME'; $c_acct = 'username'; open (PASS, 'location/of/file/that/has/password/in/it') || die "Can't get passwd"; read PASS, $c_passwd,20; close PASS; $dbh = DBI->connect($c_dbname, $c_acct, $c_passwd); ################################################## # DROP A TABLE CALLED TESTER ################################################## $drop_string = "DROP TABLE tester"; $sth = $dbh->prepare($drop_string); $sth-> execute(); ################################################## # CREATE A TABLE CALLED TESTER ################################################## $create_string = "CREATE TABLE tester (id integer, name Varchar(30))"; $sth = $dbh->prepare($create_string); $sth-> execute(); ################################################## # INSERT A SINGLE ROW INTO TESTER ################################################## $singlequote="'"; $comma=","; $id = 1; $name = 'Barbara'; $insert_string = "insert into tester (id, name) values ("; $insert_values = join '', $id, $comma, $singlequote , $name, $singlequote,')'; $insert_stmt = join'',$insert_string,$insert_values; $sth = $dbh->prepare($insert_stmt); $sth-> execute(); ################################################### # DEFINE THE (UPDATE STATEMENT), PREPARE THE STATEMENT AND # EXECUTE. ################################################### $newvar = "Richard"; $singlequote="'"; $comma=","; $columnvar=1; $update_string = join '',"update tester set name = ", $singlequote, $newvar, $singlequote, " where id = ",$columnvar; print "Update string is ", $update_string; $sth = $dbh->prepare($update_string); $sth-> execute(); ################################################## # DEFINE THE (DELETE STATEMENT), PREPARE THE STATEMENT AND # EXECUTE. ################################################## $delvar = 'Richard'; $singlequote="'"; $delete_string = "delete from tester"; $where_string = join '', " where name = ", $singlequote, $delvar, $singlequote; $delete_stmt = join '', $delete_string, $where_string; $sth = $dbh->prepare($delete_stmt); $sth-> execute(); ################################################# # DEFINE THE (SELECT STATEMENT), PREPARE THE STATEMENT AND # EXECUTE. #The WHILE statement allows you to process each record at a time. # Since this example only grabbed a single column from the database # there is only one element in the array (@row). Therefore $row[0] holds # the value from the first column. If two columns were referenced, the # $row[1] would contain the value of the second column. ################################################# $select_string = "select id from tester"; $sth = $dbh->prepare($select_string); $sth-> execute(); while ((@row = $sth->fetchrow())) { foreach (@row) { defined($_) or $_= "" }; $columnvar = $row[0]; } ############################################## # CLOSE THE DATABASE ############################################## $sth->finish(); $dbh->disconnect; }