sql server perl dbi odbc freetds on linux

After a *lot* of faffing about, I’ve finally managed to get Perl DBI::ODBC talking to a sql server database. For future reference, this post is useful, as is this one. The relevant config files on RHEL6 are

/etc/odbcinst.ini

[FreeTDS]
Description = FreeTDS driver (Sybase/MS SQL) v0.91
Driver = /share/apps/freetds_0.91/lib/libtdsodbc.so
Setup = /share/apps/freetds_0.91/lib/libtdsodbc.so
UsageCount = 1
FileUsage = 1

/etc/odbc.ini

[CRIS]
Driver = FreeTDS
Server = x.x.x.x
Port = 1433
TDS_Version = 7.0
Database = MyDatabaseName

/path/to/freetds/etc/freetds.conf.

[MSSQL]
host = x.x.x.x
port = 1433
tds version = 7.0

test that odbc and freetds are working first:

/path/to/freetds/bin/tsql -S MSSQL -U Username -P Password -D Databasename

isql -v MSSQL Username Password

And then in perl

use DBI;
my $dsn = ‘dbi:ODBC:DSN=CRIS;’;
my $dbh = DBI->connect($dsn, $db_user, $db_pass);
# do some stuff with database
$dbh->disconnect;

Advertisements

3 responses to “sql server perl dbi odbc freetds on linux

  1. I’m going to hunt you down and kiss you on the mouth. The ‘faffing about’ I was doing was leaving bloody scars on my forehead. There may have been references in other posts to what you showed in /etc/odbcinst.ini but I didn’t see them and that proved to be what I was lacking – that plus your elegant test script. Thanks for faffing until you were able to provide this answer. Now I can get a LOT done.

  2. I should have also mentioned, I was doing this in ubuntu – I just had to find the alternate locations:
    Driver = /usr/local/lib/libtdsodbc.so
    Setup = /usr/local/lib/libtdsodbc.so
    for the odbcinst.ini file.

  3. I did this test with this ODBC driver for SQL Server https://www.devart.com/odbc/sqlserver/
    You may try it as an alternative.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s