Re: Possible to get field names and types in a table without executing a query?
- From: mdougher@xxxxxxxx (Matthew Dougerty)
- Date: Tue, 27 Jun 2006 12:27:05 -0400
Here's one for oracle
sub GetOracleFieldLengths
{
my ($TABLE)=@_;
my (%FIELDLENGTHS, %FIELDTYPES);
if ($TABLE=~/\.(\S+)$/)
{
$TABLE=$1;
}
my $fieldlengths=$dbh->prepare("SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
from ALL_TAB_COLUMNS where TABLE_NAME=?");
$fieldlengths->execute(uc($TABLE));
while (my $columns=$fieldlengths->fetchrow_hashref)
{
$FIELDLENGTHS{$$columns{COLUMN_NAME}}=$$columns{DATA_LENGTH};
$FIELDTYPES{$$columns{COLUMN_NAME}}=$$columns{DATA_TYPE};
PrintDebug("DBDB $$columns{COLUMN_NAME} has value
$FIELDLENGTHS{$$columns{COLUMN_NAME}} and type $FIELDTY
PES{$$columns{COLUMN_NAME}}\n");
}
return (\%FIELDLENGTHS, \%FIELDTYPES);
}
On Tue, 27 Jun 2006 10:34:27 -0500, JupiterHost.Net wrote
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...
Matthew Dougherty
mdougher@xxxxxxxx
973-325-8556
AIM: mattsei
SKYPE: matthewdougherty_nj
.
- Follow-Ups:
- Re: Possible to get field names and types in a table without executing a query?
- From: JupiterHost.Net
- Re: Possible to get field names and types in a table without executing a query?
- References:
- Possible to get field names and types in a table without executing a query?
- From: Brian Barto
- Re: Possible to get field names and types in a table without executing a query?
- From: JupiterHost.Net
- Possible to get field names and types in a table without executing a query?
- Prev by Date: In search of ParamValues in error handler after a do call fails
- Next by Date: Re: Possible to get field names and types in a table without executing a query?
- 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
|
|