Last Update: 22 August 2008
Product: StarSQL
Version: 5.x
Article ID: SQV00SQ013
Perl, or Practical Extraction and Reporting Language, is an open source programming language. It is often used to create CGI programs and other web scripts, and is particularly useful for Web forms. StarSQL can be used with Perl, DBI (database interface module for Perl), and the DBD::ODBC module to access DB2 databases.
This document explains how to set up and use StarSQL and unixODBC with Perl on UNIX platforms. The same concepts should apply to using Perl on Windows. This document also provides instructions on how to create and run a CGI application written in Perl on an Apache HTTP Server.
Run all UNIX commands as a root user.
Most UNIX distributions include Perl, and you can run the following command to verify that it is installed:
# perl -v
If Perl is not installed, obtain it from either the UNIX distribution media, your UNIX vendor, or the Comprehensive Perl Archive Network, http://www.cpan.org (browse the binary distributions). You may also download ActivePerl from http://www.activestate.com.
Some UNIX distributions supply DBI and DBD::ODBC. However, in many cases you will need to build and install DBI and DBD::ODBC from source, which may be downloaded from http://www.cpan.org. To determine whether DBI and DBD::ODBC are installed, run the following commands:
# perl -e 'use DBI;'
# perl -e 'use DBD::ODBC;'
# perl -MDBD::ODBC -e 'print %DBD::ODBC::VERSION;'If DBI and DBD::ODBC are not installed, follow the procedures below to build both modules.
StarSQL is a 32-bit driver and should be used with the 32-bit versions of perl, DBI, and DBD::ODBC. Do not use StarSQL with the 64-bit versions of perl, DBI, and DBD::ODBC.
This solution explains how to set up and use StarSQL and unixODBC with Perl on UNIX platforms. The steps that you need to perform, which are explained in detail in the sections that follow, are:
# perl Makefile.PL
# make
# make test
# make install
Set the DBI_DSN, DBI_USER, and DBI_PASS environment variables to values that the DBD::ODBC tests can use to verify the database connection during the build process. Run the following commands, replacing mydsn, myuser, and mypassword with the appropriate values for the ODBC data source, DB2 userID, and DB2 password, respectively.
(using csh)
# setenv DBI_DSN dbi:ODBC:mydsn
# setenv DBI_USER myuser
# setenv DBI_PASS mypassword
# perl Makefile.PL -o %STARSQL/odbc
# make
# make test
# make install
# perl -e 'use DBI;'
# perl -e 'use DBD::ODBC;'
# perl -MDBD::ODBC -e 'print $DBD::ODBC::VERSION;'
If the modules were built successfully, the output from the last command will be the version of the DBD::ODBC module, such as shown in the following example:
# perl -MDBD::ODBC -e 'print $DBD::ODBC::VERSION;' 1.13#
The following short program will fetch the contents of a table and display the result set.
use strict;
use DBI;
my $dbh = DBI->connect('dbi:ODBC:mydsn', 'myuser', 'mypassword',
{AutoCommit => 0});
my $sql = qq/select * from MYTABLE/; # the query to execute
my $sth = $dbh->prepare($sql); # prepare the query
$sth->execute(); # execute the query
my @row;
while (@row = $sth->fetchrow_array) { # retrieve one row
print join(", ", @row), "\n";
}
$dbh->disconnect if ($dbh);
# perl test.pl
The following tasks must be performed in order to successfully use Perl, unixODBC, and StarSQL in conjunction with a Web server. The procedure outlined below was tested with the Apache HTTP Server running on Linux.
/usr/share/starsql/lib
/usr/share/starsql/odbc/lib
# /sbin/ldconfig
- Enter the following command to restart Apache.
# /etc/init.d/apache restart
# ln -s $STARSQL/lib/libICONV.so /usr/lib
#!/usr/bin/perl -w
use DBI;
# it is recommended that the application set the environment variables HOME and STARSQL
$ENV{HOME} = '/var/www';
$ENV{STARSQL} = '/usr/share/starsql';
# declare the content-type for the web server to use
print "content-type: text/html\n\n";
print '<html><head><title>Basic CGI</title><head><body>';
my $dbh = DBI->connect('dbi:ODBC:mydsn', 'myuser', 'mypassword',
{AutoCommit => 0});
my $sql = qq/select * from MYTABLE/;
# the query to execute
my $sth = $dbh->prepare($sql);
$sth->execute;
print '<table border="1">';
# table headings are SQL column names
print "<tr><th>$sth->{NAME}->[0]</th><th>$sth->{NAME}->[1]</th></tr>";
while (my @row = $sth->fetchrow_array) {
print "<tr><td>$row[0]</td><td>$row[1]</td></tr>\n";
}
print " </table>\n";
print " </body></html>\n";
# chmod 755 webtest.pl
If the Web application cannot locate Perl, use the command " which perl" to determine the installed location of Perl and modify the first line of the sample Web application (webtest.pl) if it is a location other than /usr/bin.
For errors related to the Web server, examine the Web server error log (i.e., /var/log/httpd/error_log).
If you encounter an error related to the database or the database queries, enable ODBC tracing by editing /usr/local/etc/odbcinst.ini. After reproducing the problem, examine the ODBC trace log.
The information in technical documents comes without any warranty. The author(s) or distributor(s) will not accept responsibility for any damage incurred directly or indirectly through use of the information contained in these documents. The information in technical documents may be gathered from various sources, including IBM, Microsoft, and other organizations.