Connecting to SQL Server From a Linux Environment With Perl’s DBD::Sybase

I recently had the requirement to connect to a SQL Server DB from a Linux environment using Perl. Having run a round a bit to get this working has generated an opportunity to document the procedure, hoping that it will save others a bit of time.

To begin with, it is necessary to have Perl's DBI library installed. There is the handy Bundle::DBI which contains all the modules used by the Perl Database Interface. To install, do the following:

$ perl -MCPAN -e 'install Bundle::DBI'

To give you the ability to connect to a SQL Server DB, you need a set of C libraries - the FreeTDS libraries. This library allows *nix to talk natively to both MSSQL and Sybase servers. Briefly, to configure, make and install:

$ tar -xvf freetds-stable.tgz
$ cd freetds-0.64
$ ./configure\\
> --with-tdsver=8.0\\
> --prefix=/usr/local/freetds
$ make test
All ok? Then -
$ make
$ su root
Password:
$ make install
$ make clean

Next, you are going to need Perl's DBD::Sybase, the Sybase driver for the DBI module. I had a few problems with the CPAN installation on this one, so I opted for downloading the package and installing by hand.

$tar -xvf DBD-Sybase-1.07.tar.gz
$cd DBD-Sybase-1.07
$export SYBASE=/usr/local/freetds
$perl Makefile.pl

You will be prompted with a few questions including some about your DB environment - answer as truthfully as you can! At the end stage of the Perl makefile execution there are usually a couple of complaints about missing libraries - these are not usually fatal and shouldn't therefore worry you unduly. One step that may be very important before running make, is to take a look at the makefile Perl has generated, in this case located at: /tmp/DBD-Sybase-1.07/Makefile. You may find that you need a link to the libiconv library to succeed. If so, edit as follows...

Change:

EXTRALIBS = -L/usr/local/freetds/lib -lct
LDLOADLIBS = -L/usr/local/freetds/lib -lct

to:

EXTRALIBS = -L/usr/local/freetds/lib -lct -liconv
LDLOADLIBS = -L/usr/local/freetds/lib -lct -liconv

As an aside, the DB environment information - host, username, passwd etc is located at: /tmp/DBD-Sybase-1.07/PWD. You may need to edit this if you have made any mistakes while answering those questions earlier on. You'll soon find that the make and install will fail if it can't connect to your target DB.

Once you are happy with the makefile and your DB environment info, it's then a case of running the make in test mode first - I thoroughly recommend testing because of the possibility of DB connection failure. If you have connection success, then make, make install and finally make clean.

A note on testing the setup - there are some samples provided for you to play with in a samples directory, I have reproduced one here.

#!/usr/local/bin/perl
#
use DBI;

my $dbh = DBI->connect("dbi:Sybase:server=JDBC", 'guest', 'sybase', {PrintError => 0});

die "Unable for connect to server $DBI::errstr"
unless $dbh;

my $rc;
my $sth;

$sth = $dbh->prepare("select \@\@servername");
if($sth->execute) {
while(@dat = $sth->fetchrow) {
print "@dat\n";
}
}

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blogmarks
  • co.mments
  • del.icio.us
  • digg
  • Fark
  • Furl
  • Reddit
  • Spurl
  • TailRank
  • YahooMyWeb

About this entry