How can I get the database name from a Perl MySQL DBI handle?

Harry picture Harry · Nov 21, 2008 · Viewed 7k times · Source

I've connected to a MySQL database using Perl DBI. I would like to find out which database I'm connected to.

I don't think I can use:

$dbh->{Name}

because I call USE new_database and $dbh->{Name} only reports the database that I initially connected to.

Is there any trick or do I need to keep track of the database name?

Answer

Rizwan Kassim picture Rizwan Kassim · Nov 21, 2008

Try just executing the query

select DATABASE();

From what I could find, the DBH has access to the DSN that you initially connected with, but not after you made the change. (There's probably a better way to switch databases.)