Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: MSSQL/Perl

by bilfurd (Hermit)
on Jul 02, 2003 at 20:22 UTC ( [id://270936]=note: print w/replies, xml ) Need Help??


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

Replies are listed 'Best First'.
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);

      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.

        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.

      There are a few things I found difficult with your code.

    • Firstly, Please use Code Tags around your code.
    • Next, if you had run perl -c yourscript.pl you would have seen a host of errors, for example the missing ; that has already been pointed out.
    • There also seem to be a fair number of typing errors in the code you posted.. this could be because you typed in a rush - if you tried the code you asked us to look at it would not have worked.
    • Also you have not declared $val0, $val1, $val2 or $count for that matter. I think you were trying to look at elements in the @vals array in which case you should have done something like $val[0], $val[1], $val[2]
    • You use fetchrow_hashref when reading in the records when you really want a fetchrow_array, although you could have used the fetchrow_hashref with something more along the following lines..
      while (my $vals = fetchrow_hashref) { print "name=$vals->{'name'}, wins=$vals->{'wins'}, losses=$vals->{ +'losses'}\n"; ++count; }

      So here is my untested re-write of your code with a few other changes - notice the connect string...

      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"; #This is effectively your DSN my $dbh = DBI ->connect ( "DBI:ODBC:$connect_string","$userid","$passw +ord" )or die $DBI::errstr; # You need to check which version of the D +BD::ODBC module you are using my $sql = "select name, wins, losses from teams"; my $sth = $dbh->prepare ( $sql ); $sth->execute; my $count = 0; while (my @vals = $sth->fetchrow_array ) { printf "name =%s, wins =%d, losses = %d\n", $vals[0], $vals[1], $v +als[2]; ++$count; } print "$count rows total\n"; $sth->finish (); #$data ->close (); #Don't know what this is.. probably old code..? $dbh->disconnect (); exit (0);

      HTH.

      -----
      Of all the things I've lost in my life, its my mind I miss the most.

      Apart from all the other things people have told you about this script:

      1. 'DBI:ODBC:' should be 'dbi:ODBC:'
      2. $connect_string should use uppercase for 'driver' e.g., DRIVER.
      3. I see you commented out PrintError and RaiseError and I'm guessing you did that because you couldn't work out how to pass them to connect without supplying another username and password as connect is (connect_string, username, password, attributes). You can either a) just set RaiseError/PrintError off the $dbh after connect e.g., $dbh->{PrintError} = 0 or b) take UID=$user;PWD=$password out of your $connect_string and pass them as the 2nd and 3rd args to DBI's connect then put your commented code as the 4th argument.
      4. You don't need to use DBD::ODBC. DBI loads that for you. The only time you might want to use DBD::ODBC is if you needed some symbols it does not export by default.
      5. I doubt you want to include host=localhost as I don't believe any SQL Server Server drivers use that connection attribute - but I've not seen them all.
      6. I'd be surprised if you really had an ODBC driver called "SQL Server" as I've never seen that on unix (and other comments in this thread seemed to suggest you were using unix). The string you assign to DRIVER needs to be a driver located in you odbcinst.ini file - it will be a name between square brackets - see example below.
      [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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://270936]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-23 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found