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!

In reply to Re^3: Using DBI to make connection to a database by 1nickt
in thread Using DBI to make connection to a database by campbell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.