简体   繁体   English

如何从Perl MySQL DBI句柄获取数据库名称?

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

I've connected to a MySQL database using Perl DBI. 我使用Perl DBI连接到MySQL数据库。 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. 因为我调用USE new_database$dbh->{Name}只报告我最初连接的数据库。

Is there any trick or do I need to keep track of the database name? 是否有任何技巧或我需要跟踪数据库名称?

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. 根据我的发现,DBH可以访问您最初连接的DSN,但不能在您进行更改后访问。 (There's probably a better way to switch databases.) (可能有更好的方法来切换数据库。)

$dbh->{Name} returns the db name from your db handle. $dbh->{Name}从db句柄返回db名称。

If you connected to another db after connected with your dbh, using mysql query "USE db_name", and you did not setup a new perl DBI db handle, of course, $dbh->{Name} will return the first you previously connected to... It's not spontaneic generation. 如果在连接到dbh之后连接到另一个db,使用mysql查询“USE db_name”,并且没有设置新的perl DBI db句柄,当然,$ dbh - > {Name}将返回先前连接到的第一个......这不是自发的一代。

So to get the connected db name once the db handle is set up - for DBI mysql: 因此,一旦设置了db句柄,就获得连接的db名称 - 对于DBI mysql:

sub get_dbname {  
    my ($dbh) = @_;  
    my $connected_db = $dbh->{name};  
    $connected_db =~ s/^dbname=([^;].*);host.*$/$1/;  
    return $connected_db;  
}  

You can ask mysql: 你可以问mysql:

($dbname) = (each %{$dbh->selectrow_hashref("show tables")}) =~ /^Tables_in_(.*)/;

Update: obviously select DATABASE() is a better way to do it :) 更新:显然选择DATABASE()是一个更好的方法:)

When you create a connection object it is for a certain database. 创建连接对象时,它适用于某个数据库。 In DBI's case anyway. 无论如何,在DBI的情况下。 II don't believe doing the SQL USE database_name will affect your connection instance at all. 我不相信做SQL USE database_name会影响你的连接实例。 Maybe there is a select_db (My DBI is rusty) function for the connection object or you'll have to create a new connection to the new database for the connection instance to properly report it. 也许连接对象有一个select_db(我的DBI是生锈的)函数,或者您必须创建一个到新数据库的新连接,以便连接实例正确报告它。

FWIW - probably not much - DBD::Informix keeps track of the current database, which can change if you do operations such as CREATE DATABASE. FWIW - 可能不多 - DBD :: Informix跟踪当前数据库,如果您执行CREATE DATABASE等操作,则可能会更改。 The $dbh->{Name} attribute is specified by the DBI spec as the name used when the handle is established. $dbh->{Name}属性由DBI规范指定为建立句柄时使用的名称。 Consequently, there is an Informix-specific attribute $dbh->{ix_DatabaseName} that provides the actual current database name. 因此,有一个特定于Informix的属性$dbh->{ix_DatabaseName} ,它提供实际的当前数据库名称。 See: perldoc DBD::Informix . 请参阅: perldoc DBD::Informix

You could consider requesting the maintainer(s) of DBD::MySQL add a similar attribute. 您可以考虑请求DBD :: MySQL的维护者添加类似的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM