Downloading and installing
Perl
The Active version
of Perl is well documented and
the latest
build as of this writing is available
as a
freeware download
from http://www.activestate.com
The installation
is the simplest process of all and any
Windows aware
user should be able to do that quite
easily.
Enabling CGI access
No ..No.. i am not explaining
that. This is a slightly tricky
issue and explaining
that would extend the length and
make me
to divert from the topic. This
heading is
included to
make the reader aware that
it is one
essential part
to make an interactive communication
possible with the web server,
which is the basis for all
the database
programming over the web. Complete
documentation is
available, which comes along with the
Perl download and
it could be referred for enabling CGI
access with various Web Servers.
Following
this
link
would
also help you
access the reference available from the
activestate site.
You can in addition use this link,
in
case you are using Microsoft
Internet Information Server
or Microsoft Personal Web
Server.
The Database connectivity
To establish database
connectivity you need to have
an ODBC compliant
database, and a Perl extension
that provides
access to the ODBC
Application
Programming Interface
(API) . Win32::ODBC is a Perl 5
extension written
by Dave Roth and is available from
ftp.roth.net
. The installation instructions alone
are
repeated below from
the official
page to Win32::ODBC
the other
details such as the
Sql statements
supported, Features,troubleshooting
etc., can be had by
clicking the link.
Installing Win32::ODBC
1. Copy the ODBC.PM file into the
\PERL\SITE\LIB\WIN32
directory.
2. Copy the
ODBC10x.PLL file into
the
\PERL\SITE\LIB\AUTO\WIN32\ODBC
directory. This is
either ODBC105.PLL
(for versions of Perl 105 or before)
or ODBC106.PLL
(for versions of Perl 106 or later),
renaming the
correct file to ODBC.PLL (run "perl -v" to
check which version
of perl you have).
After successfully installing
Win32::ODBC you need to
create a System DSN.
Follow these steps carefully to
do it.
Creating a System DSN
1. Double-click the MyComputer Icon
and then open the
Control-Panel
on your system.
2. Locate the ODBC Manger icon and
double-click on it.
The icon looks
like this
3. Click the System DSN
tab and click Add. You will
be prompted to
select the ODBC driver, that you will
be using, from
the list of available drivers. If your
database is MS-Access
then choose the Microsoft
Access Driver(*.mdb)
or choose the suitable driver
according to
your database. This is followed by a dialog
box asking you
to enter the DataSource Name (DSN).
Type in any applicable
name. This is the name you will
be using
in your programs to talk with your database.
This should be
unique. Then select the database to
associate with
this Datasource.
4. If you require
you can also give a username and
password to connect
to the Datasource by choosing the
Advanced tab.
5. This completes the successful
creation of a System DSN.
Now moving on to some real programming.
I'll explain an example
code that should be enough to keep
you get going.
Consider a situation where you would
like to query a database
for the Author's name and
price of the book titled "CGI Programming
with Perl".
1. use Win32::ODBC;
2. $db=new WIN32::ODBC('simple');
3. $varname="CGI Programming with
Perl";
4. $db->Sql("select aname,price
from mytable where
book='$varname' ");
5. $db->FetchRow();
6. ($Author,$cost)=$db->Data("aname","price");
7. print "The Book titled
$varname is written by $Author
and costs $cost";
Line 1: Instruction
to the Perl Interpreter to include the package
Win32::ODBC.
Line 2: Creates a database
handle to the Datasource 'simple'.
Line 3: Assigns the
title of the book to the variable '$varname'.
Line 4: Passes a SQL
Query to the table 'mytable' through the
handle '$db'.
Line 5: Fetches the
First row of record that matches the Query.
Line 6: Assigns the
value of the fields 'aname' (Author name)
and 'price' to the variables $Author and $cost respectively.
Line 7: Prints the Result
successfully.
Hope that was easy enough.
Let us proceed to see , how the same is implemented under the unix platform.