Is this connection object a separate object than DBI ?

DBI is a class, not an object. The connection object ($dbh) is an instance of the DBI class. They are not the same thing, but the DBI class defines how its instances behave.

Falling back on the standard analogy to animals, Dog - as in the concept of Dog, not a specific dog - is a class and your pet $fido is an instance of Dog. Dog and $fido are separate, but connected. You wouldn't say "$fido is Dog", but rather "$fido is a Dog".

In the same way, $dbh isn't DBI, but it is a DBI.

Does this connection object have a method prepare ?

Yes. The DBI class defines a prepare method, which is then available to all instances of the class. Going back to Dog and $fido, being able to bark is part of the definition of Dog, therefore, because $fido is a Dog, $fido can bark.

I am confused that , how can connection have a method ?

Well... That's a matter of abstraction, I'm afraid.

The connection object is not literally the database connection itself. It is an object which contains the connection, keeps track of the connection, and is used to represent the connection to allow for easier interaction with it. We do tend to talk about the connection object as if it were the actual connection, but it isn't, because that allows it to be much more flexible and easier to use, since we can do things like add methods to a connection object instead of having to deal directly with the raw data flowing between our programs and the databases they connect to.


In reply to Re^3: dbi question by dsheroh
in thread dbi question by manishrathi

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.