Search the web
Sign In
New User? Sign Up
Perl_Official · Perl . CGI . Shell script
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Getting Microsoft SQL data through ODBC and Perl?   Message List  
Reply | Forward Message #2036 of 2062 |
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::Dumper;
use DBI;

print "Content-type:text/html\n\n";

#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::errstr\n"; # $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";
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 "$sql\n";

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{"EMAIL"} . "\n";

}

return 1;

print"Test 7 <BR>";





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

print"Test 9<BR>";

End of code.
Thanks
Chip




Tue Aug 14, 2007 8:42 pm

chipwillis2003
Offline Offline
Send Email Send Email

Forward
Message #2036 of 2062 |
Expand Messages Author Sort by Date

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...
chipwillis2003
Offline Send Email
Aug 15, 2007
8:43 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help