David N Murray wrote:

If you don't know the columns that will be returned, I presume you're
doing something akin to 'select * from'.  At any rate, if you know the
table name, why not ask the database for the table info?

#!/usr/bin/perl
use DBI;
my $dbh = DBI->connect("dbi:mysql:test",'','');
die "Unable to connect:\n" . $DBI::errstr . "\n" if (! defined($dbh));

# 3rd param is table name, 4th means all columns
my $sth = $dbh->column_info(undef, undef, 'test', '%');
while (my @ar = $sth->fetchrow_array()) {
   print join(":", @ar), "\n";
}
$sth->finish;
$dbh->disconnect;

which is akin to
mysql> desc test;

HTH,
Dave


Yeah I've thought about that.
The problem is that people can use field aliases, eg:

select concat(FirstName, ' ', LastName) as FullName from MyTable
or even
select ID as MyID from MyTable

so I need to be able to grab the field name as it will be returned from the database server.

I was kinda hoping that even though a 'blank' recordset was being returned, that I could still *somehow* pull some field names out of it. And I'd also like to make it DB-neutral - which is one of the reasons I'm shying away from using a LIMIT clause. I suppose I can figure out why DB server is being used and create a DB-specific query on-the-fly, eg SQL Server would want a "select TOP 1 * from MyTable" instead of MySQL's "select * from MyTable LIMIT 1".

--
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: [EMAIL PROTECTED]
website: http://www.nusconsulting.com.au

Reply via email to