Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Using DBI to make connection to a database

by campbell (Beadle)
on Jul 02, 2015 at 13:08 UTC ( [id://1132973]=note: print w/replies, xml ) Need Help??


in reply to Re: Using DBI to make connection to a database
in thread Using DBI to make connection to a database

Sorry - didn't mean it that way, just meant that as I'm a newbie with DBI it may take me a long while (that I don't have) to get it cracked, and that if an uber-user could crack it in a few seconds, that would be miles more efficient...
  • Comment on Re^2: Using DBI to make connection to a database

Replies are listed 'Best First'.
Re^3: Using DBI to make connection to a database
by 1nickt (Canon) on Jul 02, 2015 at 13:46 UTC

    No worries. Nice of you to reply back.

    It's common to have these sorts of problems when trying to connect to a DB with the DBI. The problems usually have to do with escaping and interpolation in perl. Try these links given in a recent thread. Because you can connect to your DB using ODBC manager or whatever, with the same credentials you think you are giving in your $DSN, it's tricky to understand what the problem could be. Often the DBI error message is long and/or incomplete or confusing. As I say, this is a common DBI beginner's mistake.

    What you can learn here are ways to become a better programmer, not just immediate solutions to what you're stuck on (although you'll often get those if you ask nicely).

    So that's why my first answer (and that of a another respondent) was to advise you to start debugging your program as you write it. Print out $DSN and you will see if it has any quoting/interpolation problems. Get into the habit of examining all your data as you develop your program.

    One way to do this is declare a global variable that turns debugging on or off, and then you can have conditional debugging statements in your code that are only printed when the switch is on. (Using && to test true for the first half of the statement before executing the second half...)

    #!/usr/bin/env perl -w use strict; use 5.010; use DBI; my $DEBUG = 1; &db_connect( 'foo', 'bar', 'baz' ); sub db_connect { my ($srvr, $usr, $pwd) = @_; my $DSN = "dbi:ODBC:Server=$srvr; UID=$usr;PWD=$pwd"; $DEBUG && say $DSN; my $dbh=DBI->connect($DSN, { RaiseError => 1 }); } __END__

    I write most code with this built in, whether the debug flag is set at the top of a script, or in a config file, or in an environment variable, or whatever. Change one value somewhere and the whole thing turns on or off. You can use it to enable/disable logging, send/not send email while testing, whatever. And you can easily have more than one level of debugging and "escalate" certain things that you really want to examine when all else is working. Once it's working okay, set $DEBUG to 0 and it shuts up. Next time there's a problem, or you want to add or change something, it's all there ready to turn back on.

    Good luck!

    Remember: Ne dederis in spiritu molere illegitimi!

Log In?
Username:
Password:

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

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

    No recent polls found