Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I am trying to learn simple database programming but am getting stuck. All I want to do is print to screen all the table names in the database when the user presses the submit button. I thought this code would work;
print $cgi->h4 ("Use the web browser to select tables to view!"); print $cgi->start_form (-method => "POST"); print $cgi->submit (-name => "submit", -value => "Browse"); print $cgi->end_form (); print $cgi->end_html (); $dbh->disconnect (); $sth = $dbh->prepare (qq{ SHOW TABLES; }); $sth->execute (); while (my @ary = $sth->fetchrow_array ()) { print join ("\t", @ary), "\n"; }
WOuld anyone be able to suggest how I can modify my code to do this?? Thanks ;-)

Replies are listed 'Best First'.
Re: my first database
by rdfield (Priest) on Feb 13, 2003 at 14:32 UTC
    To be honest, this looks like a random selection of lines from the CGI and DBI pods :)

    First up, SHOW TABLES isn't an SQL statement, so don't use that: something like $dbh->table_info is more like it.

    Second up, you issue a disconnect to your database before you try to prepare your "SQL". There's no chance of that working at all - if you add  or die $dbh->errstr after prepare and execute statements you might get some better information (you are checking the webserver log file?)

    Third up, you're issuing an end_html command and then printing some more lines (spaced with tabs - which a browser wouldn't honour anyway).

    Fourth up, even if you move the end_html after the db related prints, it's going to print them out whether or not the submit is pressed - you need something like if (defined($cgi->param('submit')) {print ...} in there somewhere.

    I suspect that you're in for a steep learning curve - have a look in Tutorials: there are several well written articles on the DBI and CGI topics.

    rdfield

Re: my first database
by jasonk (Parson) on Feb 13, 2003 at 14:26 UTC

    The code you have there will work fine, as long as you get rid of that call to disconnect() that disconnects you from the database before trying to prepare a query. Other databases probably won't use the same 'SHOW TABLES' command that mysql does though, if you want it to be portable you probably want to look at the DBI table_info() method.

      table_info is supposed to be the most portable eventually, it is currently experimental, and very few drivers implement it.

      You guys should really investigate "Statement Handle Attributes" in the DBI pod. Even those depend on the underlying driver, but since they've been a part of DBI longer, they are more portable.

      I recently started using DBD-SQLite, and after converting my mysql-specific sql into equivalent ANSI92 DBD-SQLite compliant using code from Re: MySQL 2 SQLite, I came up with the following to dump some metadata about my database ;)

      use DBI; use Data::Dumper; my $dbh = DBI->connect("dbi:SQLite:pod.wiki.sqlite.db") or die $DBD::e +rr; warn "database name is ", $dbh->{Name}; my $sth = $dbh->prepare("SELECT * FROM content limit 0"); warn $sth->execute; for my $attr( qw[ NUM_OF_PARAMS NUM_OF_FIELDS NAME NAME_hash TYPE PRECISION SCALE NULLABLE]){ eval { warn $attr, ' ', Dumper($sth->{$attr}); }; warn $attr,' is unavailable (', 1||$@, ')' if $@; } __END__ database name is pod.wiki.sqlite.db at dbi.table.info.pl line 5. 3 at dbi.table.info.pl line 7. NUM_OF_PARAMS $VAR1 = 0; NUM_OF_FIELDS $VAR1 = 5; NAME $VAR1 = [ 'name', 'version', 'text', 'modified', 'comment' ]; NAME_hash $VAR1 = { 'text' => 2, 'modified' => 3, 'comment' => 4, 'version' => 1, 'name' => 0 }; TYPE is unavailable (1) at dbi.table.info.pl line 11. PRECISION is unavailable (1) at dbi.table.info.pl line 11. SCALE is unavailable (1) at dbi.table.info.pl line 11. NULLABLE is unavailable (1) at dbi.table.info.pl line 11.
      update: ++ That is all true, but if you're looking for a list of tables, soon you'll be looking to describe those tables, and ... you get the point, yes? yes ;)


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
      ** The Third rule of perl club is a statement of fact: pod is sexy.

        Statement handle attributes contain information on the structure of a table, he is looking for a list of tables, which isn't available in that manner, and which your sample code doesn't provide.

Re: my first database
by CountZero (Bishop) on Feb 13, 2003 at 14:39 UTC

    Surely this is not the full code of your application (or even of this webpage)?

    Where does $dbh come from?
    Why do you disconnect from your database before even connecting?
    What kind of database are you using?
    Does it support the "SHOW TABLES" query?
    etc ...

    Give us some more info and we might be able to help you.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law