I am trying to use ODBC on a linux box to connect to a MSSQL database
running on an NT server.  I have installed unixODBC, and created a DSN
entry in /usr/local/etc/odbc.ini.  I then run isql (which comes with
unixODBC) with that DSN name, and I am able to issue SQL queries against
the database successfully.
     Now I need to get it working in perl.  I am running perl 5.005_03
with DBI-1.15 and DBD-ODBC 0.28 installed.  I am trying to execute a
simple query with the script at the end of the email (also attached).
I pass the dsn as the same one I use on the isql command line (MSSQLTEST)
which is what I have in my odbc.ini.  I then get:

DBD::ODBC::st execute failed: [unixODBC][Driver Manager]Data source name not found, 
and no default driver specified (SQL-IM002)(DBD: 
describe/SQLColAttributes/SQL_COLUMN_LENGTH err=-1) at ./test2.pl line 19.
DBD::ODBC::db disconnect failed: (DBD: db_disconnect/SQLDisconnect err=-1) at 
./test2.pl line 28.


#!/usr/bin/perl

use DBI;

$dsn = shift;
$query = shift;
$dbd = "dbi:ODBC:$dsn";

print STDERR "Connecting to DSN $dsn\n";
$db = DBI->connect($dbd);
if(!defined $db)
 {
  print $DBI::errstr,"\n";
  exit 1;
 }
print "Query: $query\n";

$h = $db->prepare($query);
$h->execute;
$save = $count = $h->rows;
while($count > 0)
 {
  @row = $h->fetchrow;
  $row = join("|",@row);
  print "$row\n";
 }
$h->finish;
$db->disconnect;

test2.pl

PGP signature

Reply via email to