Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to connect to an SQL2000 Server via Perl. I found the code that someone posted on thi site and it helped me to get the DBI to at least try to connect but I get this error: "server" is not exported by the DBD::ODBC module "database" is not exported by the DBD::ODBC module "user" is not exported by the DBD::ODBC module "password" is not exported by the DBD::ODBC module My connect string has the proper settings fot the server, database, user, and password. Does anyone have any ideas? I set up an account on this site but it won't let me log in so please send reply to cpu4u@cpunet.net along with the posting here. Thanks, glider

Replies are listed 'Best First'.
Re: MSSQL/Perl
by rnahi (Curate) on Jul 02, 2003 at 20:11 UTC

    Without seeing the code you are using, it would be difficult to give you any advice.

    Try to reduce your connection script to the minimum that gives you such error and we'll try to help.

Re: MSSQL/Perl
by bilfurd (Hermit) on Jul 02, 2003 at 20:22 UTC
    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

      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.

        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