Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DBI fetchrow array/ general Perl oo question - quickie

Status
Not open for further replies.

jimineep

Technical User
May 16, 2006
20
GB
Hi guys

I'm trying to work out if a table exists in my database, using an object which inherits from DBI.

my $TableExists = $dbh->fetchrow_array;
if ( $TableExists = "NO") { warn "No table";}

Now my question is...surely there is a way of combining the $dbh->fetchrow_arraty into the if statement, instead of having to assign it to the variable and then evaluate that.

Kind regards,
 
Code:
if ($TableExists=$dbh->fetchrow_array) {
} else {
  warn "No Table";
}
This depends on what's been passed as a query to the database, if it's a zero return of rows from a valid query, then the tables exist, just no data to match your criteria

--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I'm wondering what your query is such that it returns "No" when a table doesn't exist. If you want to evaluate the statement in a single line in an if, have it return the table name if it exists and nothing if it doesn't.

Here's a super simple query to do exactly what you want:
Code:
SHOW TABLES LIKE 'target_table_name'

If the table doesn't exist, it doesn't return anything, so you can do your warn very easily:
Code:
warn "No Table" unless ($dbh->fetchrow_array);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top