in reply to (jeffa) Re: DBI and MySQL wild card function?
in thread DBI and MySQL wild card function?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: (jeffa) Re: DBI and MySQL wild card function?
by diotalevi (Canon) on Oct 02, 2002 at 03:44 UTC | |
Watch closely as I boggle at your statement. Boggle. Why, er, how are you working with a table where you don't know what the columns are? I can imagine not knowing the column names in advance if you are writing some generic database code that is expected to work with multiple tables or something like that. I included some code form a project I'm working on that demonstrates such a generic function with some PostgreSQL specific bits added in. The only reason I'd expect jeffa to deprecate 'select * ...' is that you can easily chew up memory if you aren't careful. In my case I get back a single row so there's no problem. Message me if you'd like to see the rest of the code.
__SIG__ | [reply] [d/l] [select] |
|
(jeffa) 3Re: DBI and MySQL wild card function?
by jeffa (Bishop) on Oct 02, 2002 at 05:51 UTC | |
I just try to avoid select * in any serious 'production' type code that i might churn out. Sure, i have been known to be lazy and use select * (i've also been know to use dot star in my regexes!), but i (hopefully) will turn right around and replace the * with the column names. Now, consider this silly but possible example: Say you have two tables, both with a field named 'id' and a field named 'title':See the difference? By avoiding select * (or just being happy with an array or array ref) and explicitly specifying the columns, you leave open the option to rename those columns, allowing you to safely store the results in a hash without worry of clobbering existing keys. So, how do you select the columns when you don't know ahead of time what they will be. Most likely in a vendor-specific way. Here is one way to do it with MySQL:
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) | [reply] [d/l] [select] |
by diotalevi (Canon) on Oct 02, 2002 at 06:33 UTC | |
Oh and here I'd assumed that if someone was going to use SELECT * that they obviously didn't care about the column order. Heck at that point you're delegating your column order to the database engine and it's possible that a new schema would reorder things if not actually alter names. I prefer to just get the column names from the statement handle but that's just personal style. __SIG__ | [reply] [d/l] [select] |
|
Re: Re: (jeffa) Re: DBI and MySQL wild card function?
by Anonymous Monk on Oct 02, 2002 at 11:51 UTC | |
Well, I wanted to include 50 columns, Didn't know select * would hurt it. I guess I should just type all 50 column names out? i.e select id, clte, claimno, date, name, city, etc. Anthony | [reply] |
by diotalevi (Canon) on Oct 02, 2002 at 15:45 UTC | |
Yes, you type them out or do something like Re: Re: (jeffa) Re: DBI and MySQL wild card function? where I do a SELECT ..., * but don't care about the order since they values just get stuck into a single hash object. There the trick is to use the statement handle's knowledge of the column names and either do it as a single assignment (hash slice and the preferred method) or if you prefer tedium, iterate over the row. __SIG__ | [reply] [d/l] [select] |