Once you've connected to your database with connect(), you can run any
valid SQL query (and whatever non-standard MySQL querys) you want by
calling prepare(SQL query) on the database object, and then execute() on
the prepare'ed object. Confused? Maybe not, but I'm sure someone out there
is.
Time for an example:
use DBI;
# Define some variables like $user, $password, $database...
$dbh = DBI->connect("DBI:mysql:$database", $user, $password) || die "bad";
$sth = $dbh->prepare("SHOW TABLES");
$sth->execute();
# Then use fetchrow() (or fetchrow_hashref(), or fetchrow_arrayref()) to
# get the next row of results.
while ($result = $sth->fetchrow) {
print $result . "\n";
}
If I haven't left anything out (I've cut and pasted just to make sure it
runs), this will print out all of the tables found in whatever database
you're looking in.
Have fun.
Johnathan Thibodeau
Pound bang user bin perl
fork while true
On Thu, 31 May 2001, Michael Risser wrote:
> I'm trying to write a Perl script which will connect to a MySQL database and
> create certain tables if they do not already exist. Is there a way to send a
> query such as "SHOW TABLES" and read the result into a variable for parsing?
>
> --------------------------------------------
> Michael D. Risser
> Machine Vision Products, Inc.
> 760-438-1138 ext 107
> --------------------------------------------
> -- Due to the current economic situation, the light at the end of the tunnel
> will be turned off until further notice.
>