Skip to main content

Connecting to SQL Server from OS X perl

I've been spending my coding time in the offhours working on Perl instead of Ruby. My coding time in general has been very limited, which is part of the reason for the length of time between updates. :)

My latest project is to pull data out of a Microsoft SQL Server database for analysis. I'm using perl for various reasons: I need a crossplatform environment, and I need certain libraries that only work on perl. Some of the target users for my code run on Windows.

I know that Ruby runs on Windows but it's not the platform of choice for Ruby developers. The vast majority seem to develop either on OS X or Linux. So Ruby on Windows isn't at the maturity that ActiveState perl is on Windows.

In fact, I don't even run native perl anymore on my MacBook Pro. I've switched over to ActiveState perl because I don't need to compile anything every time I want to install new CPAN libraries. And because it's ActiveState, I'm that much more confident it will work on other platforms.

The bottom line is that perl makes the most sense for what I'm trying to do. I vastly prefer ruby to perl but I don't mind working in perl when I have to.

So how to connect to SQL Server from perl? My first thought was that I could use ODBC. My research quickly took me to DBD::ODBC in CPAN. After spending some time Googling for other examples and trying to get it working, I wasn't getting anywhere.

It took me some more research until I realized that DBD::ODBC is only one piece of the whole picture. I also need an ODBC driver manager and an ODBC driver.

The two main ODBC driver managers for Unix/Linux are unixODBC and iODBC. Fortunately, iODBC is already included in OS X.

For the ODBC driver itself, I wound up using FreeTDS. Because FreeTDS is only available as source code, I had to use MacPorts to download and compile the code. MacPorts installs everything in /opt/local by default.

So here was the process:

1. Install FreeTDS. If you are using ActiveState perl as I am, you must force MacPorts to build FreeTDS as 32-bit because ActiveState is 32-bit only. If you go with the default of compiling FreeTDS as 64-bit (or x86_64) then you will get this error when you call the FreeTDS library code from ActiveState perl:

[iODBC][Driver Manager]dlopen(/opt/local/lib/libtdsodbc.so, 6): no suitable image found. Did find:
/opt/local/lib/libtdsodbc.so: mach-o, but wrong architecture (SQL-00000) at test.pl line 26

So to fix this error, edit the MacPorts configuration file:

sudo vi /opt/local/etc/macports/macports.conf

Uncomment this line:

set build_arch i386

Once you build and install FreeTDS, ensure that you can use it to talk with the database. I used tsql:

TDSVER=8.0 tsql -H 10.10.10.1 -p 1433 -U 'DOMAIN\user' -P 'password'

2. After you do this, test your ODBC driver manager and driver. I used iODBC because it comes with OS X. It comes with a utility called iodbctest. I used similar parameters to tsql when testing:

iodbctest "Driver=/opt/local/lib/libtdsodbc.so;Server=10.10.10.1;Port=1433;TDS_Version=8.0;uid=DOMAIN\user;pwd=password;Database=Database"

Note a few things:
  • Other sites tell you to make entries in odbc.ini, odbcinst.ini, or freetds.conf. If you set all parameters on the command line then you don't need to tweak these other config files.
  • The Driver parameter is set to the full path for libtdsodbc.so. This is the actual ODBC driver. The name or path may differ, depending on your OS and ODBC driver software.
  • I haven't created an ODBC data source on my Windows SQL Server host so far. Some sites say you need to do this but I found it worked without this.
3. Use the perl code with DBD::ODBC to call the database. Here's some sample code. Note how I first populate the data source ($dsn) with the parameters I used with iodbctest, and then I pass this to the DBI->connect method.
#!/usr/bin/perl

use strict;
use DBI;

my $user = 'user';
my $pass = 'password';
my $driver = "/opt/local/lib/libtdsodbc.so";
my $db_server = "10.10.10.1";
my $db_name = 'Database';
my $port = 1433;
my $tds_version = "8.0";

my $dsn = join "", ("dbi:ODBC:",
   "Driver=$driver;",
   "Server=$db_server;",
   "Port=$port;",
   "UID=$user;",
   "PWD=$pass;",
   "TDS_Version=$tds_version;",
   "Database=$db_name",
);

my $db_options = {PrintError => 1, RaiseError => 1, AutoCommit => 0, };

my $dbh = DBI->connect($dsn, $db_options);
$dbh->{LongReadLen} = 32768;
my $sql = qq/select * from table/;
my $sth = $dbh->prepare($sql);
$sth->execute();
my @row;
while (@row = $sth->fetchrow_array) {
    print join(", ", @row), "\n";
}
Note on 12/29/10: I added this after the DBI->connect: $dbh->{LongReadLen} = 32768; I was getting "Data truncated" errors when accessing ntext fields. This link was a real help and helped me solve the problem.

Comments

  1. Im Immo Cashflow Booster erfahren Sie, wie Sie durch smarte Immobilienanlagestrategien passiven Cashflow generieren. Sie müssen keine Bankgeschäfte tätigen, Geld leihen oder etwas kaufen.

    Eric erklärte 6 verschiedene Strategien, die verwendet werden können, um durch Leasing und Leasing einen Cashflow zu generieren. Diese Methode ist völlig neu und eigentlich eine interne Strategie.

    Anfangs war ich dieser Strategie etwas skeptisch gegenüber, denn wenn man die vermietete Wohnung wieder untervermieten möchte, wie überzeugt man dann den Vermieter.

    Aber ich habe eine bessere Ausbildung und Eric hat klare Anweisungen, wie man den Vermieter leicht überreden kann. Der Immo Cashflow Booster bietet dir Top aktuelle Strategien, welche auch jahrelang funktionieren. Und in diesem Erfahrungsbericht, werde ich dir meine persönlichen Immo Cashflow Booster Erfahrungen mitteilen.

    ReplyDelete
  2. Just 99 Web Design offers affordable web design packages for every business, from basic one-page websites to full eCommerce website design services. affordable ecommerce website design

    ReplyDelete
  3. BOLAHIT adalah platfrom terpercaya yang menyediakan akses link situs slot777 gacor dan rtp slot88 resmi terlengkap serta selalu di perbarui setiap hari.

    ReplyDelete
  4. BOLAHIT merupakan sebuah platform yang menyediakan informasi mengenai layanan SBOBET88 serta berbagai jenis taruhan olahraga yang tersedia secara daring. Situs ini menghadirkan akses alternatif bagi pengguna yang mengalami kendala saat membuka halaman utama akibat pembatasan akses di wilayah tertentu. Selain itu, platform ini memuat penjelasan mengenai berbagai format taruhan, termasuk mix parlay, yang memungkinkan pengguna memahami cara kerja penggabungan beberapa pilihan pertandingan dalam satu tiket taruhan.

    Menjelang penyelenggaraan Piala Dunia FIFA 2026, minat masyarakat terhadap informasi seputar jadwal pertandingan, statistik tim, performa pemain, dan berbagai jenis taruhan olahraga diperkirakan meningkat. Banyak platform informasi olahraga menyediakan pembaruan mengenai kompetisi tersebut, termasuk analisis pertandingan, data historis, serta penjelasan mengenai istilah-istilah yang umum digunakan dalam dunia taruhan olahraga. Informasi tersebut dapat membantu pembaca memahami mekanisme berbagai jenis taruhan tanpa mendorong mereka untuk berpartisipasi.

    Selain menyajikan informasi mengenai kompetisi internasional, platform seperti ini umumnya menyediakan panduan penggunaan layanan, tata cara mengakses akun, serta penjelasan mengenai fitur-fitur yang tersedia. Pengguna juga dapat menemukan pembaruan terkait jadwal pertandingan, perubahan format kompetisi, dan informasi umum lainnya yang berkaitan dengan sepak bola internasional. Dengan penyajian informasi yang terstruktur dan mudah dipahami, pembaca dapat mengikuti perkembangan Piala Dunia 2026 serta memahami berbagai konsep yang berkaitan dengan taruhan olahraga dari sudut pandang yang bersifat informatif.

    ReplyDelete
  5. BOLAHIT menyediakan link alternatif situs slot 5000 resmi terpercaya dengan sistem yang stabil dan pengalaman bermain yang lebih menguntungkan.

    ReplyDelete
  6. BOLAHIT merupakan situs slot 5000 resmi yang menghadirkan pengalaman bermain slot online dengan layanan yang dirancang untuk memberikan kenyamanan, kemudahan, dan keamanan bagi setiap pemain. Dengan sistem transaksi deposit melalui DANA yang cepat dan terpercaya, para pengguna dapat melakukan top up saldo kapan saja tanpa proses yang rumit. Platform ini mengutamakan kemudahan akses sehingga pemain dapat menikmati berbagai permainan slot favorit hanya dalam beberapa langkah sederhana.

    bolahit menyediakan beragam pilihan permainan dari provider populer yang dikenal memiliki kualitas grafis tinggi, fitur menarik, serta peluang hadiah yang kompetitif. Seluruh permainan dapat diakses melalui perangkat desktop maupun smartphone, sehingga pemain bebas bermain di mana saja dan kapan saja. Sistem yang stabil serta dukungan layanan pelanggan yang responsif juga menjadi nilai tambah untuk memberikan pengalaman bermain yang lebih nyaman.

    Selain menawarkan transaksi yang praktis, BOLAHIT juga rutin menghadirkan berbagai promo menarik, bonus harian, dan program loyalitas yang dapat dimanfaatkan oleh para anggota. Setiap fitur dirancang untuk memberikan nilai lebih bagi pemain yang ingin menikmati hiburan slot online secara maksimal.

    Dengan mengedepankan keamanan data pengguna dan proses transaksi yang efisien, BOLAHIT berupaya menjadi pilihan bagi para penggemar permainan slot online. Platform ini terus meningkatkan kualitas layanan dan menghadirkan inovasi agar pengalaman bermain tetap menyenangkan. Bagi pemain yang mencari situs slot 5000 resmi dengan deposit via DANA yang mudah dan terpercaya, BOLAHIT menawarkan solusi praktis untuk menikmati berbagai permainan slot online dalam satu platform yang nyaman dan mudah diakses.

    ReplyDelete

Post a Comment