I've connected to a MySQL database using Perl DBI. I would like to find out which database I'm connected to.
我使用Perl DBI连接到MySQL数据库。我想找出我连接的数据库。
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?
是否有任何技巧或我需要跟踪数据库名称?
5 个解决方案
#1
13
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.)
根据我的发现,DBH可以访问您最初连接的DSN,但不能在您进行更改后访问。 (可能有更好的方法来切换数据库。)
#2
2
$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;
}
#3
1
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()是一个更好的方法:)
#4
0
When you create a connection object it is for a certain database. In DBI's case anyway. I I don't believe doing the SQL USE database_name
will affect your connection instance at all. 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.
创建连接对象时,它适用于某个数据库。无论如何,在DBI的情况下。我不相信做SQL USE database_name会影响你的连接实例。也许连接对象有一个select_db(我的DBI是生锈的)函数,或者您必须创建一个到新数据库的新连接,以便连接实例正确地报告它。
#5
0
FWIW - probably not much - DBD::Informix keeps track of the current database, which can change if you do operations such as CREATE DATABASE. The $dbh->{Name}
attribute is specified by the DBI spec as the name used when the handle is established. Consequently, there is an Informix-specific attribute $dbh->{ix_DatabaseName}
that provides the actual current database name. See: perldoc DBD::Informix
.
FWIW - 可能不多 - DBD :: Informix会跟踪当前数据库,如果您执行CREATE DATABASE等操作,它可能会发生变化。 $ dbh - > {Name}属性由DBI规范指定为建立句柄时使用的名称。因此,有一个特定于Informix的属性$ dbh - > {ix_DatabaseName},它提供实际的当前数据库名称。请参阅:perldoc DBD :: Informix。
You could consider requesting the maintainer(s) of DBD::MySQL add a similar attribute.
您可以考虑请求DBD :: MySQL的维护者添加类似的属性。
#1
13
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.)
根据我的发现,DBH可以访问您最初连接的DSN,但不能在您进行更改后访问。 (可能有更好的方法来切换数据库。)
#2
2
$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;
}
#3
1
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()是一个更好的方法:)
#4
0
When you create a connection object it is for a certain database. In DBI's case anyway. I I don't believe doing the SQL USE database_name
will affect your connection instance at all. 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.
创建连接对象时,它适用于某个数据库。无论如何,在DBI的情况下。我不相信做SQL USE database_name会影响你的连接实例。也许连接对象有一个select_db(我的DBI是生锈的)函数,或者您必须创建一个到新数据库的新连接,以便连接实例正确地报告它。
#5
0
FWIW - probably not much - DBD::Informix keeps track of the current database, which can change if you do operations such as CREATE DATABASE. The $dbh->{Name}
attribute is specified by the DBI spec as the name used when the handle is established. Consequently, there is an Informix-specific attribute $dbh->{ix_DatabaseName}
that provides the actual current database name. See: perldoc DBD::Informix
.
FWIW - 可能不多 - DBD :: Informix会跟踪当前数据库,如果您执行CREATE DATABASE等操作,它可能会发生变化。 $ dbh - > {Name}属性由DBI规范指定为建立句柄时使用的名称。因此,有一个特定于Informix的属性$ dbh - > {ix_DatabaseName},它提供实际的当前数据库名称。请参阅:perldoc DBD :: Informix。
You could consider requesting the maintainer(s) of DBD::MySQL add a similar attribute.
您可以考虑请求DBD :: MySQL的维护者添加类似的属性。