Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Using DBI, you access the recordset in two ways:

  • Either you use fetchrow_* methods with a loop, and then you get one record at the time. In this case, the first one in your loop is the first in the data set.
  • ... or you access all the records at once, with fetchall_*, selectcol_*, or selectall_* methods. In this case the result is a bi-dimensional array reference, and the first record is the element of the array with index 0.

A word of caution, though. If you are referring to the first physical record in a table, then be aware that there is no such a thing in a relational database. You can have a record where a specific column (e.g. the primary key) is "1", but it does not mean that this is the "first" record, whatever you mean by that.

The data set that you are "navigating" with the DBI is the result of a query, which can be influenced by WHERE and ORDER BY clauses. This data set is not guaranteed to have any relation whatsoever with the physical order of the records in your tables. There are some DBMS that implement a "record number" or similar attribute, but it is not standard, not portable, not relational.

See also the discussion following this node for some practical examples.

And, I almost forgot. You are using a "fetchrow" record. It does not exist. It is not a documented method. The methods at your disposal are "fetchrow_array," "fetchrow_arrayref," or "fetchrow_hashref."

Update
Thanks to cfreak, who points out that "fetchrow" exists and it is an alias for "fetchrow_array", even though it is not documented.
I acknowledge it, but I still think it should be better to use one of the named methods I listed above, for two reasons:

  • there is also a "fetch" method that is alias for "fetchrow"arrayref." Even though I know that, I use the explicit method, to know for sure what the return value is. I can't tell at first glance what "fetch" and "fetchrow" are returning. Therefore, in the principle of defensive programming, I stick to the docs.
  • The DBI is continuously evolving. Even documented features of DBI can sometimes change. One more reason for being conservative.

See Reading from a database in our Tutorials for more information.

_ _ _ _ (_|| | |(_|>< _|

In reply to Re: Going to first record on a record set in DBI by gmax
in thread Going to first record on a record set in DBI by chuleto1

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-19 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found