Tell you what. I'll actually address the problem you're describing, rather than blowing you off. and as for DBI tutorials, you're soaking in them!

You're only getting half of what you need. DBI is the "Database Interface" half. The part I think you're missing (based on the error messages you report, and the process you've described) is the DBD -- "Database Driver". Look on PPM or CPAN for DBI::ODBC.

See, much like ODBC, DBI only describes a common set of methods for accessing data stores. You need download the drivers which actually implement those methods. As always, CPAN has your hookup
Once you've got that installed, you can use

$dbh = DBI->connect('dbi:ODBC:DSN', 'user', 'password') || die ("Can't + connect to database : ",$DBI::errstr);
to connect to your database. You may need to alter the dbi:ODBC:DSN portion depending on how your database is set up.
Also note the $DBI::errstr; when debugging database problems, this variable is your best friend -- it'll report database errors, not perl errors, like $! and $@ do.
once the connection's made and the $dbh is live, you can start preparing SQL :
$sql = $dbh->prepare ("SELECT field FROM table") or die ("Unable t +o prepare statement: ",$DBI::errstr);
Again, notice the die. If this statement dies, there's typically a problem with the statement's syntax, or the database handle ($dbh) is undefined.

This is where the fun happens :

$sql->execute()or die ("Unable to execute get_servertime : ",$DBI: +:errstr);
This will set the SQL gnomes scurrying, and stores the result of the statement in $sql.

Now that the results are in, how to read them? It depends. There's several methods of doing so, and each one is detailed in the documentation. My most often used one is

@l_row = $sql->fetchrow_array;
, but there's other methods of doing so. There's a nice hash method which'll store the field names as the hash keys, so you can manipulate$row{fieldname} if that's clearer than working with array elements.

When everything's done and said,

$dbh->disconnect
to close the database. For further information, see Programming the Perl DBI, and of course, DBI's own POD, which has a synopsis of how to connect and read to a database.

OK?


In reply to (boo) Re: Re: Re: Can't locate DBD/ODBC.pm in @INC by boo_radley
in thread Can't locate DBD/ODBC.pm in @INC by Fian

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.