Re: Possible to get field names and types in a table without executing a query?
- From: mlists@xxxxxxxxxxxxxxx (JupiterHost.Net)
- Date: Tue, 27 Jun 2006 10:34:27 -0500
brian.barto@xxxxxxxxxxxxxxxxxxx wrote:
Hi all. My goal is to get a list of all field names and data types for
those fields in any given table in a mysql database. From reading the
DBI documentation, the only way I've been able to do this is by
preparing and executing a query against a table first. Then I would use
Instead of trying to hack together the data bases on an assumed behavior and pseodo query, do a query that actually actually asks for what you want.
For instance with MySQL:
print Dumper $dbh->select_all_arrayref('SHOW TABLE STATUS FROM "db" LIKE "table"');
# same as SHOW COLUMNS FROM db.table
print Dumper $dbh->select_all_arrayref('DESCRIBE db.table');
HTH :)
$sth->{NAME} and $sth->{TYPE} to get the field names and their
respective data types. it looks like this:
$sth = $dbh->prepare("SELECT * FROM $table") or die "Can't prepare
statement!\n";
$sth->execute or die "Can not execute statement!\n";
@types = @{$sth->{TYPE}};
@cols = @{$sth->{NAME}};
PS: use strict and warnings or you're going to have headaches when reusing $sth @types, @cols, etc...
.
- Follow-Ups:
- Re: Possible to get field names and types in a table without executing a query?
- From: Matthew Dougerty
- Re: Possible to get field names and types in a table without executing a query?
- References:
- Prev by Date: Re: Possible to get field names and types in a table without executing a query?
- Next by Date: In search of ParamValues in error handler after a do call fails
- Previous by thread: Re: Possible to get field names and types in a table without executing a query?
- Next by thread: Re: Possible to get field names and types in a table without executing a query?
- Index(es):
Relevant Pages
|
|