in reply to MSSQL/Perl
Is it possible to see the code you are using?
I think at least part of the problem is the connection string you are using. Use 'UID' for 'USER' and 'PWD' for 'PASSWORD'
my $db = DBI->connect("DBI:ODBC:driver={SQL Server};server=dbServer01;database=testdb;uid=bilfurd;pwd=testPassword;")
I will e-mail this, as well
Re: Re: MSSQL/Perl
by Anonymous Monk on Jul 02, 2003 at 22:06 UTC
|
Here it the test script I tried to use to test my connection of the DBD::OBDC
use strict;
use DBI;
use DBD::ODBC
my ($data_source, $database, $userid, $password) =qw( server webdb web
+dev webdevpass );
my $connect_string = "driver={SQL Server};Server=$data_source;Database
+=$database;UID=$user;PWD=$password";
my $dbh = DBI ->connect ( "DBI:ODBC:host=localhost;$conn_string" )or d
+ie $DBI::errstr;
# {PrintError => 0, RaiseError =>1});
my $sql = "select name, wins, losses from teams"
$sth = $dbh->prepare ( $sql );
$sth->execute;
$count = 0;
while (my @val = $sth->fetchrow_hashref )
{
printf "name =%s, wins =%d, losses = %d\n",
$val[0], $val[1], $val[2];
++$count;
}
print "$count rows total\n";
#$sth->finish ();
#$data ->close ();
$dbh->disconnect ();
exit (0);
| [reply] [d/l] |
|
This code can't compile.
You are missing a semicolon after use DBD::ODBC
and after my $sql ... and some variables don't match with their declaration ($user $userid, $connect_string $conn_string). You didn't declare $sth and $count.
Moreover, you are assigning the result of fetchrow_hashref
to an array.
You should read Reading from a database to get started
with the DBI, and maybe some of the more basic Tutorials
to use the language more proficiently.
And, please, use CODE tags when you post your code.
Update
Here is your code a little bit better formatted. I won't guarantee that it does what you want, but at least it compiles without errors.
use strict;
use DBI;
#use DBD::ODBC ;
my ($data_source, $database, $userid, $password) =
qw( server webdb webdev webdevpass );
my $connect_string =
"driver={SQL Server};Server=$data_source;Database=$database;UID=$useri
+d;PWD=$password";
my $dbh = DBI ->connect ( "DBI:ODBC:host=localhost;$connect_string" )
or die $DBI::errstr;
my $sql = "select name, wins, losses from teams";
my $sth = $dbh->prepare ( $sql );
$sth->execute;
my $count = 0;
while (my @val = $sth->fetchrow_array ) {
printf "name =%s, wins =%d, losses = %d\n",
$val[0], $val[1], $val[2];
++$count;
}
print "$count rows total\n";
$dbh->disconnect ();
I changed your code to use fetchrow_array instead of fetchrow_hashref.
You really need to read some docs. The DBI documentation (perldoc DBI) will give you good references on how to use things. The tutorial I have mentioned above is a good start. It is true that it uses MySQL, but using DBI, the only thing that is (in principle) different is only the connection statement. After that, the methods are the same.
Good luck.
| [reply] [d/l] [select] |
|
Thanks rnahi for pointing out my errors in the code and the formatting error I used in posting my code.
The errors in the code, when corrected only made for more errors when it was run. And the code reference that you politely pointed out to me is all well and good if I were using mySQL but I'm not. I am using MS SQL2000, OS is Win2000, and IIS 5.
I have been going around in circles with no luck connecting to SQL2000.
Thanks again and any futher help will be appretiated.
| [reply] |
|
|
| [reply] [d/l] [select] |
|
[Easysoft ODBC-SQL Server]
Driver=/usr/local/easysoft/sqlserver/lib/libessqlsrv.so
Setup=/usr/local/easysoft/sqlserver/lib/libessqlsrvS.so
Threading=0
FileUsage=1
DontDLClose=1
UsageCount=27
| [reply] [d/l] |
|
|