tags:

views:

125

answers:

3

Is there a nice simple way to get the amount of columns using perl and mysql? I generate the sql select code so i dont know how many columns are in there. I do have the column section isolated so a regex solution could be simple?

-edit- for the comment below.

"select " . col . " FROM " #more code
col = "a, b, c, d" #how do i find out if theres 4 columns?
query->column_count(); #returns 4
col = "a, b, c.c, d, e, f"
query->column_count(); #return 6 etc.
A: 

$column_amount = @row; #i cant believed no one replied

An employee
We need a new badge - answered own question in under 15 minutes. :)
Deverill
-1 for both the question and the answer. Neither the question nor the answer was posed in a way people other than you could interpret it unambiguously. I quickly lose patience with such questions and prefer to spend my time figuring answers to more meaningful questions.
Sinan Ünür
huh, whats wrong with the above? i asked bc i dont know. (i didnt think the above would work and had to lookup how to get the length of an array)
An employee
@An employee: Trust me, your question is cryptic at best. And your answer is something any Perler could have told you had they understood your question. But no one replied because you didn't explain what you wanted clearly enough.
Axeman
@An employee see also http://meta.stackoverflow.com/questions/24838/are-you-less-likely-to-down-vote-someone-whose-gravatar-is-a-photograph-of-them/24862#24862
Sinan Ünür
Sinan Ünür: i didnt...
An employee
@An employee: Sorry. My bad. Dunno what happened.
Sinan Ünür
Axeman: Something just hit me... A perl programmer, not being able to read a statement that is 'cryptic at best'!
An employee
@An employee: what's wrong with the above is that there is no @row in the question; the question implies (but leaves ambiguous) that you want to get the column count for some arbitrary string of "columns" *before* executing the query.
ysth
A: 

I'm sure there's a DBI method to get the information you seek. But, meh, I don't really feel like looking for it (see comments in other answer).

Ether
+2  A: 

You can get the list of columns returned in the 'NAME' attribute of your statement.

Here is an example:

 my $query = "SELECT * FROM table";
 my $qr = $dbi->prepare($query);

 $qr->execute();
 my $columns = $qr->{'NAME'};
 printf "%d columns retuned\n", scalar @$columns;
Ivan Nevostruev