Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Cannot connect to SQL Server 2000 using DBI

by skyler (Beadle)
on Jul 04, 2004 at 02:58 UTC ( [id://371650]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, I need your help. I'm trying to connect to a SQL Server 2000 database and I'm having problems with it. when I run the script, it gets an error message "Can't locate method "Connect" via package "DBI" Perhaps you forgot to load DBI at line 39" Could you let me know what is it wrong in the script?? Could you give me an example how to connect to SQL Serve 2000 using DBI?? I'll appreciate your help.
#! perl -w use strict; use warnings; use DBI; # my $dir = 'K:\\reports'; # my $count = 0; # opendir DH, $dir or die "Cannot open $dir: $!"; # while (my $file = readdir DH) { # next unless $file =~ /\.pdf$/; # my @newfile = substr($file,2,6); # process_records(@newfile); # } # closedir DH; my $user = "username"; my $password = "password1"; my $data_source = "dbi::SQL Server:Interfaces"; my $dbh = DBI->connect($data_source, $user, $password, { PrintError => 1, RaiseError => 0 } ) or die "Can't connect to $data_source: $DBI::errstr"; my $sth = $dbh->prepare( q{ SELECT * FROM tableview where field1 = '029622'}) or die "Can't prepare statement: $ +DBI::errstr"; my $rc = $sth->execute or die "Can't execute statement: $DBI::errstr"; print "Field names: @{ $sth->{NAME} }\n"; while (my @arr = $sth->fetchrow_array) { print "@arr\n"; } die $sth->errstr if $sth->err; $dbh->disconnect;

Replies are listed 'Best First'.
Re: Cannot connect to SQL Server 2000 using DBI
by WhiteBird (Hermit) on Jul 04, 2004 at 03:35 UTC
    Have you confirmed that DBI is loaded on your system? (What platform? What version of Perl?)

    You also should add a DBD module. When connecting to a SQL Server I tend to use DBD::ODBC. Look into the documentation. Do a super search on DBI and DBD::ODBC.

      I thought in the connection string, you need to specify the driver of connecting database??? Why do I need to add DBD???
        I suppose the simpliest way to say it is that the DBD is the driver to the database. If I understand the process correctly, the DBD contains the specifics for the particular database that you are trying to connect to. You need to understand both DBI and your chosen driver. I haven't pursued the "why" as much as the "how". You just need all the correct pieces or the connection isn't going to work. Have you looked at the DBI site? You can download a DBD there and get more information.
Re: Cannot connect to SQL Server 2000 using DBI
by Anonymous Monk on Jul 04, 2004 at 03:22 UTC
    my $data_source = "dbi::SQL Server:Interfaces";
    dbi::?
      do you have any sample on how to best connect to SQL Server 2000 uisng DBI??
        The AM is right, you should have a DBD driver defined between the first and the second colon.

        Assuming your perl code runs on Windows... I think the easiest way is to use the DBD::ODBC driver. Make a System-DSN to your database, with the "32-bit ODBC data sources" control panel from Windows, and use the name you gave it there, as the third part for your connect string. If that name was "Foo" (just a silly example :-) ), the connect string can then look like:

        "DBI:ODBC:Foo"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found