A simple naming convention could be a big help here. Anytime
i create a database table, i always (99% of the time) use
an "auto increment" id field named
id. Not
cust_id. Not
user_id. Just
id.
After all, if i know the name of the table, i know what
kind of id it is. Now, if that id is referenced in another
table, then i append the full table name. Example:
foo: id
name
status
bar: id
name
status
foo_id
By keeping a convention such as this, i look up field names
less often, and i can also take advantage of this in my
code, especially when having to insert into more than one
table at a time (second and third normalized tables).
Class::DBI is a difficult module to master, but it
powerfully abstracts such details away and allows you
retrieve rows easily:
my $cd = CD->retrieve(1);
my @cds = CD->retrieve_all;
my @cds = CD->search(year => 1980);
my @cds = CD->search_like(title => 'October%');
But once you have to start joining tables together, things
can get a bit tricky. It is worth taking a look at, however.
UPDATE:
Oh, in the spirit of TIMTOWTDI, here is my version of a
generic insert:
sub generic_id_fetch {
my ($table,$id,@field) = @_;
my $field = @field ? join(',',@field) : '*';
$dbh->selectrow_hashref("select $field from $table where id=?",unde
+f,$id);
}
And yes, i hardcoded the name of the
id field.
YMWV (your mileage will vary). Hope this helps. :)
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.