Getting started with ODBC........7 August 1998
I wanted to use Perl, the Win32::ODBC module, and Microsoft's ODBC software to
connect to a database....
It took several days of reading newsgroups, looking at web sites, and
some very helpful email responses from all over the world before it all
clicked into place.
I was looking for simple scripts which worked! Most of what I found was
too complicated and I could not "see the wood for the trees"... This
page is meant to help anyone else trying to get started.
It goes like this;
- I am using Windows 95....
- I installed
OmniHTTPd (v1.01) local web site software. The file at the bottom of
their page. Created a directory for my html files and a sub-directory for
the cgi scripts. I configured OmniHTTPd as follows.
- Server
Site Address: 127.0.0.1
Server Root: c:\html (you can use your own path of course..)
- Standard CGI
Virtual: /cgi-bin
Actual: c:\html\cgi-bin (following my own path given above)
- Windows CGI (as for Standard CGI.
- Advanced
Enable Perl CGI Support
Perl CGI Command Line: c:\perl\bin\perl.exe
- I created a small, simple database using MS Access. To use my perl
script below "as is" you would need to create records which contain at
least the first and last name of each person (called FirstName and
LastName). You can of course alter the script to match your own
database.
- Make sure that you have the ODBC icon in Control Panel. If not
install it. It comes with Internet Explorer 4, so you could just install
this. Otherwise get it from
Microsoft.
- I configured ODBC for my database.
- ..opened up the ODBC icon.
- Selected "User DSN". (for CGI over Internet - I'm advised should use
System DSN - have not tried that out yet)
- Highlighted the name opposite the Microsoft Access Driver [*mdb] entry
and clicked on Configure.
- Gave my database a "Data Source Name", ie "contacts".
- Clicked on "Select" and put in the name of my database ("geoff.mdb") and
the path to it, ie c:\html\
- Clicked on OK.
- Closed the ODBC icon.
- Installed the ActiveState Perl
software (file pw32a316.zip) in c:\perl
- Obtained the Win32::ODBC module from
www.roth.net. Clicked on the Win32::ODBC Perl 5 extension link and then the "Get it" link.
Moved into the 970208 directory, then the Bin directory and downloaded
the file called Win32_ODBC_Build_311.zip. Installed module by following
the instructions for a Windows 95 installation in the README file which
come with the zip file. They are...
- Copy the ODBC.PM file into the directory perl\lib\win32\
- Rename the file ODBC_XXX.PLL to ODBC.PLL
- Make a directory: perl\lib\auto\win32\odbc\
- Copy the ODBC.PLL file into the directory in the step above
- Created an html file which asks a question of the database. Look at
the source for this file and copy/save the HTML between the dotted lines
to a file called open.htm in c:\html\
-------------------------------------------------------
open test
Name Search
------------------------------------------------------
- Created a Perl script, which uses the Win32::ODBC module to
interogate the database and returns the result...Look at the source
for this file and copy/save the HTML between the dotted lines
to a file called open.pl and copy it into your cgi-bin folder,
c:\html\cgi-bin
You will remember from above that the field names in my database
for the first and last name are FirstName and LastName.
The information which really helped me here is in the
ODBC FAQ. Suggest you
look at this after trying out my scripts.
------------------------------------------------------
use Win32::ODBC;
print "Content-type: text/html\n\n";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
print "\n";
print "\n";
print "test open\n";
print "\n";
print "\n";
$DSN="contacts";
if (!($db=new Win32::ODBC($DSN))) {
print "Error connecting to $DSN\n";
print "Error: " . Win32::ODBC::Error() . "\n";
}
else {
print "Database being searched...
\n";
}
$SqlStatement = "SELECT * FROM contacts";
if ($db->Sql($SqlStatement)) {
print "SQL failed.\n";
print "Error: " . $db->Error() . "\n";
}
else {
while($db->FetchRow()) {
%Data = $db->DataHash();
if ($Data{"FirstName"} eq $value) {
print " \n";
print "Full name: " . "$value " . $Data{"LastName"} . "\n";
print " \n";
}
}
}
$db->Close();
exit;
print "\n";
print "\n";
-----------------------------------------------------
- Load Internet Explorer and enter the following URL to get started
127.0.0.1/open.htm
There might be mistakes, there will certainly better ways of doing what
I have done! If you have any comments please email me at
geoff.cox@cableinet.co.uk
Cheers
Geoff