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;

  1. I am using Windows 95....
  2. 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.
  3. 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.
  4. 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.
  5. I configured ODBC for my database.
  6. Installed the ActiveState Perl software (file pw32a316.zip) in c:\perl
  7. 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...
  8. 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\ ------------------------------------------------------- <html> <head> <title>open test</title> </head> <body> <h2>Name Search</h2> <form action="/cgi-bin/open.pl" method="post"> This database contains info on three people! - Jim, Fred and Jane.<p> To find Full name enter First name? <p> <input name="name" size="40" type="text"><p> <input type="submit" value="Send"><p> <input type="reset" value="Clear"> </form> </body> </html> ------------------------------------------------------
  9. 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 "<HTML>\n"; print "<HEAD>\n"; print "<TITLE>test open</TITLE>\n"; print "</HEAD>\n"; print "<BODY bgcolor=\"ffffcc\">\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...<br><br><br>\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 "<b> \n"; print "Full name: " . "$value " . $Data{"LastName"} . "\n"; print "</b> \n"; } } } $db->Close(); exit; print "</BODY>\n"; print "</HTML>\n"; -----------------------------------------------------
  10. 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