List Info

Thread: Getting Microsoft SQL data through ODBC and Perl?




Getting Microsoft SQL data through ODBC and Perl?
country flaguser name
United States
2007-08-14 15:42:46

Hi,
I am fairly new to Perl and have written some Perl to interface
with an API that comes with an application i am working with. I now
need to bypass the API and go directly to the Microsoft SQL server
data base. The SQl server is the same box that the Perl code is on.
i am Admin on the box. I have set up an ODBC and have gotten some
code from the web to get me started. i have gotten to the following
part where I have note the Perl code stops.
I have two questions.

One is how can i get better debugging than printing out test data on
different lines?

And of course why does this code not work?

See below:

#!/usr/bin/perl -w
use URI::Escape;
use LWP::UserAgent;
use XML::Simple;
use Data:umper;
use DBI;

print "Content-type:text/htmlnn";

#print "test 1";

#SQL_test_1.pl

# Function: dbh_connect
# Description: Creates a connection to a database
# Arguments: none
# Returns: $db_conn - database connection handle
#


$db_user = "sa";
$db_pass = "sa55";
$dsn_name = 'dbi:ODBC:iLincEmailAddress';


sub dbh_connect
{
my ($dbh);

$dbh = DBI->connect($dsn_name, $db_user, $db_pass, {
PrintError => 0,
AutoCommit => 1
});

if (! defined($dbh) )
{
print "Error connecting to DSN '$dsn_name'n";
print "Error was:n";;
print "$DBI::errstrn"; # $DBI::errstr is the
error
# received from the SQL
server

return 0;
}

return $dbh;
}

dbh_connect;
print";Test 2<BR>;";

#Opens DB ODBC connection

if (! dbh_connect() )
{
print "Unable to connect to database.n";

exit; # Unable to reconnect, exit the script
gracefully
}
else
{
print "DSN connected to database.n <BR>";
}

print";Test 3 <BR>";

my ($sql, $sth, $rc);

$sql = shift;

if (! ($sql) )
{
print ("Must pass SQL statement to
db_sql!n");

return 0;
}


print "Test 4 <BR>";

my ($sql, $sth, $href);

$sql = "SELECT EMAIL FROM LMS_USER&quot;;
print "$sql <BR>";






#********************************************************************
****************************************
# failing here <----------------------------------------
---------------------------> failing here
$sth = $dbh->prepare($sql); # Prepare the SQL statement passed
to db_sql

$sth = db_sql($sql); # Pass the SQL statement to the server
print "Test 5 <BR>";

if ( ! defined( $sth ) || ! ($sth) )
{
print "Unable to prepare SQL
statement:n";

print "$sqln";

return 0;

}

$rc = $sth->execute;
print "Test 6";


#
# Check that we received a statement handle
#
if (! ($sth) )
{
return 0;
}

while( $href = $sth->fetchrow_hashref )
{
print "EMAIL: " . $$href{&quot;EMAIL&quot;} . "n&quot;;

}

return 1;

print&quot;Test 7 <BR>";

print"Test 8<BR>;";
return $sth;

print";Test 9<BR>;";

End of code.
Thanks
Chip

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )