如何获取数据库中存在的表名称
我有一个代码,我想获取数据库信息并存储所需的数据,我已经连接到数据库,我想要的是获取数据库中存在的表的名称,以便我可以访问它们一一获取所需的信息。
这是
#!/usr/bin/perl
use DBI;
use strict;
use warnings;
use Switch;
use Data::Dumper;
use Spreadsheet::Read qw(ReadData);
my @expValues ="";
my $portDes="";
my $portValue="";
my $ttidDes;
my $ttidValue;
my $tidvDes;
my $tidvValue;
my $tiDes;
my $tiValue;
my $redirectHostDes;
my $redirectHostValue;
my $saQueryDes;
my $saQueryValue;
my $dialedNumDes;
my $dialedNumValue;
my $dbType = "mysql";
my $database = "control";
my $host="localhost";
my $dsn="";
my $userid="";
my $password="";
if($dbType eq "mysql"){
$dsn = "DBI:$dbType:database=$database;host=$host:port=XXXX";
$userid = "XXXX";
$password = 'XXXX';
}else{
$dbType="postgres";
$userid = "postgres";
$password = "postgres";
$dsn = "DBI:$dbType:database=$database;host=$host,port=XXXX";
}
my $dbcon = DBI->connect($dsn, $userid, $password) or die "Can't connect to database: $DBI::errstr\n";
print "connected to the database\n";
my $notables = $dbcon->tables();
print "No of tables : $notables" ;
sub getXlxsDetails(){
my $book = ReadData ($_[0]);
my $configrows = $_[1];
#print $configrows;
my @rows;
#my @port;
#my @ttid;
#my @ti;
#my @tidv;
#my @redirect;
#my @saQuery;
#my @dialed;
#my @returnInfo;
if($configrows eq ""){
$portValue="Null";
$ttidValue="Null";
$tidvValue="Null";
$tiValue="Null";
$redirectHostValue="Null";
$saQueryValue="Null";
$dialedNumValue="Null";
print ("NOT NULL**************************");
return $portValue,$ttidValue,$tidvValue,$tiValue,$redirectHostValue,$saQueryValue,$dialedNumValue;
}
else{
if($configrows eq 'Development'){
@rows = Spreadsheet::Read::rows($book->[1]);
}
if($configrows eq 'Production'){
@rows = Spreadsheet::Read::rows($book->[2]);
}
foreach my $i (1 .. scalar @rows) {
foreach my $j (1 .. scalar @{$rows[$i-1]}) {
my $key = $rows[$i-1][$j-1] // '';
switch($key){
case "Port Assigned " {
print ("MATCHED************************** Port Assigned\n");
$portDes = (($rows[$i-1][$j-1])."\n");
$portValue = (($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j] = ($portValue);
}
case "TTID (Y/N, default: Y)" {
print ("MATCHED************************** TTID (Y/N, default: Y)\n");
$ttidDes= (($rows[$i-1][$j-1])."\n");
$ttidValue=(($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j+1] = ($ttidValue);
}
case "TIDV (Y/N, default: Y)" {
print ("MATCHED************************** TIDV (Y/N, default: Y)\n");
$tidvDes=(($rows[$i-1][$j-1])."\n");
$tidvValue= (($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j+2] = ($tidvValue);
}
case "TI (Y/N, default: Y)" {
print ("MATCHED************************** TI (Y/N, default: Y)\n");
$tiDes= (($rows[$i-1][$j-1])."\n");
$tiValue=(($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j+3] = ($tiValue);
}
case "Redirect host FQDN (default)" {
print ("MATCHED************************** Redirect host FQDN (default)\n");
$redirectHostDes=(($rows[$i-1][$j-1])."\n");
$redirectHostValue=(($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j+4] = ($redirectHostValue);
}
case "SA-Query URL" {
print ("MATCHED************************** SA-Query URL\n");
$saQueryDes=(($rows[$i-1][$j-1])."\n");
$saQueryValue=(($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j+5] = ($saQueryValue);
}
case "Dialed Number (from SIP invite)" {
print ("MATCHED************************** Dialed Number (from SIP invite)\n");
$dialedNumDes= ($rows[$i-1][$j-1]);
$dialedNumValue=($rows[($i + 1)-1][$j-1]);
#@returnInfo[$j+6] = ($dialedNumValue);
}
}
next;
}
}
return $portValue,$ttidValue,$tidvValue,$tiValue,$redirectHostValue,$saQueryValue,$dialedNumValue;
}
#@returnInfo = (\@port,\@ttid,\@ti,\@tidv,\@redirect,\@saQuery,\@dialed);
}
#my $Result=&getXlxsDetails(\@rowsDev,"Port Assigned ");
my ($result1,$result2,$result3,$result4,$result5,$result6,$result7)=&getXlxsDetails('024_Goldman_Sachs.xlsx','Development');
print($result1."\n".$result2."\n".$result3."\n".$result4."\n".$result5."\n".$result6."\n".$result7);
#print "@$result\n";
print "\n";
我所获得的输出的代码:
(tc-lab-04) /home/trustid/switchadaptor/testauto/dataverificationtesting2/XL_Parsing>./aa.pl
**connected to the database**
No of tables : 1MATCHED************************** Port Assigned
MATCHED************************** SA-Query URL
MATCHED************************** TTID (Y/N, default: Y)
MATCHED************************** TIDV (Y/N, default: Y)
MATCHED************************** TI (Y/N, default: Y)
MATCHED************************** Redirect host FQDN (default)
MATCHED************************** Dialed Number (from SIP invite)
10272
Y
Y
N
ivr.localdomain
https://devapi-primary.trustidinc.com/tid
8332259639
- >它给我不。桌子,而不是否。在表中,我想获取数据库中所有表的名称。
我已经使用过的代码片段:
my @names = $dbcon->tables( $catalog, $schema, $table, $type );
print Dumper @names;
我正在使用虚拟机,基本上是我的Office Lab Server& DB部署在该服务器中。
I have a piece of code where I wanted to fetch the database information and store the required data, I am already connected to the database what I am looking for is to fetch the name of the tables present inside the database so that I can access them one by one and fetch the required information.
Here's my code
#!/usr/bin/perl
use DBI;
use strict;
use warnings;
use Switch;
use Data::Dumper;
use Spreadsheet::Read qw(ReadData);
my @expValues ="";
my $portDes="";
my $portValue="";
my $ttidDes;
my $ttidValue;
my $tidvDes;
my $tidvValue;
my $tiDes;
my $tiValue;
my $redirectHostDes;
my $redirectHostValue;
my $saQueryDes;
my $saQueryValue;
my $dialedNumDes;
my $dialedNumValue;
my $dbType = "mysql";
my $database = "control";
my $host="localhost";
my $dsn="";
my $userid="";
my $password="";
if($dbType eq "mysql"){
$dsn = "DBI:$dbType:database=$database;host=$host:port=XXXX";
$userid = "XXXX";
$password = 'XXXX';
}else{
$dbType="postgres";
$userid = "postgres";
$password = "postgres";
$dsn = "DBI:$dbType:database=$database;host=$host,port=XXXX";
}
my $dbcon = DBI->connect($dsn, $userid, $password) or die "Can't connect to database: $DBI::errstr\n";
print "connected to the database\n";
my $notables = $dbcon->tables();
print "No of tables : $notables" ;
sub getXlxsDetails(){
my $book = ReadData ($_[0]);
my $configrows = $_[1];
#print $configrows;
my @rows;
#my @port;
#my @ttid;
#my @ti;
#my @tidv;
#my @redirect;
#my @saQuery;
#my @dialed;
#my @returnInfo;
if($configrows eq ""){
$portValue="Null";
$ttidValue="Null";
$tidvValue="Null";
$tiValue="Null";
$redirectHostValue="Null";
$saQueryValue="Null";
$dialedNumValue="Null";
print ("NOT NULL**************************");
return $portValue,$ttidValue,$tidvValue,$tiValue,$redirectHostValue,$saQueryValue,$dialedNumValue;
}
else{
if($configrows eq 'Development'){
@rows = Spreadsheet::Read::rows($book->[1]);
}
if($configrows eq 'Production'){
@rows = Spreadsheet::Read::rows($book->[2]);
}
foreach my $i (1 .. scalar @rows) {
foreach my $j (1 .. scalar @{$rows[$i-1]}) {
my $key = $rows[$i-1][$j-1] // '';
switch($key){
case "Port Assigned " {
print ("MATCHED************************** Port Assigned\n");
$portDes = (($rows[$i-1][$j-1])."\n");
$portValue = (($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j] = ($portValue);
}
case "TTID (Y/N, default: Y)" {
print ("MATCHED************************** TTID (Y/N, default: Y)\n");
$ttidDes= (($rows[$i-1][$j-1])."\n");
$ttidValue=(($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j+1] = ($ttidValue);
}
case "TIDV (Y/N, default: Y)" {
print ("MATCHED************************** TIDV (Y/N, default: Y)\n");
$tidvDes=(($rows[$i-1][$j-1])."\n");
$tidvValue= (($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j+2] = ($tidvValue);
}
case "TI (Y/N, default: Y)" {
print ("MATCHED************************** TI (Y/N, default: Y)\n");
$tiDes= (($rows[$i-1][$j-1])."\n");
$tiValue=(($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j+3] = ($tiValue);
}
case "Redirect host FQDN (default)" {
print ("MATCHED************************** Redirect host FQDN (default)\n");
$redirectHostDes=(($rows[$i-1][$j-1])."\n");
$redirectHostValue=(($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j+4] = ($redirectHostValue);
}
case "SA-Query URL" {
print ("MATCHED************************** SA-Query URL\n");
$saQueryDes=(($rows[$i-1][$j-1])."\n");
$saQueryValue=(($rows[$i-1][($j+1)-1])."\n");
#@returnInfo[$j+5] = ($saQueryValue);
}
case "Dialed Number (from SIP invite)" {
print ("MATCHED************************** Dialed Number (from SIP invite)\n");
$dialedNumDes= ($rows[$i-1][$j-1]);
$dialedNumValue=($rows[($i + 1)-1][$j-1]);
#@returnInfo[$j+6] = ($dialedNumValue);
}
}
next;
}
}
return $portValue,$ttidValue,$tidvValue,$tiValue,$redirectHostValue,$saQueryValue,$dialedNumValue;
}
#@returnInfo = (\@port,\@ttid,\@ti,\@tidv,\@redirect,\@saQuery,\@dialed);
}
#my $Result=&getXlxsDetails(\@rowsDev,"Port Assigned ");
my ($result1,$result2,$result3,$result4,$result5,$result6,$result7)=&getXlxsDetails('024_Goldman_Sachs.xlsx','Development');
print($result1."\n".$result2."\n".$result3."\n".$result4."\n".$result5."\n".$result6."\n".$result7);
#print "@$result\n";
print "\n";
The output I am getting is:
(tc-lab-04) /home/trustid/switchadaptor/testauto/dataverificationtesting2/XL_Parsing>./aa.pl
**connected to the database**
No of tables : 1MATCHED************************** Port Assigned
MATCHED************************** SA-Query URL
MATCHED************************** TTID (Y/N, default: Y)
MATCHED************************** TIDV (Y/N, default: Y)
MATCHED************************** TI (Y/N, default: Y)
MATCHED************************** Redirect host FQDN (default)
MATCHED************************** Dialed Number (from SIP invite)
10272
Y
Y
N
ivr.localdomain
https://devapi-primary.trustidinc.com/tid
8332259639
--> It is giving me no. of table, instead of no. of tables I wanted to fetch the name of all the tables present in the database.
Code snippets I already used:
my @names = $dbcon->tables( $catalog, $schema, $table, $type );
print Dumper @names;
I am working on the Virtual Machine, basically it is my office lab server & the DB is deployed in this server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
$ dbcon-> table()
在问题中尝试的确实是一种很好的方法,但是返回表格列表 - 虽然将其分配给标量,因此您可以获得表的数量,如所观察到的。†,也将其称为无参数(请参见下面链接的文档)。 Dave Sherohman在答案中显示了一种正确的使用方法。或者,请使用 table_info /metacpan.org/dist/dbi/view/dbi.pm#tables“ rel =” nofollow noreferrer“>表格是“ 简单接口”),以获取和查看更多详细信息不仅仅是名称,并查看哪个表适合您的目的
,
或者甚至
忽略了最后一个
'table'
(并使用'''或undef
)然后,view
s也将包含在resutls中。†这个问题也显示了正确
的
Using
$dbcon->tables()
as attempted in the question is a good way indeed, but that returns the list of tables -- while it is assigned to a scalar so you get the number of tables, as observed.† Also, calling it without parameters is deprecated (see docs linked below). A correct way to use this is shown in the answer by Dave Sherohman.Or, use table_info (that tables is a "simple interface to"), to obtain and review more detailed information than just the names, and see which of the tables are suitable for your purpose
or with
or even
If you leave out that last
'TABLE'
(and use''
orundef
instead) then theVIEW
s will be included in the resutls as well, what may be of interest.† The question also shows the correct
but it doesn't tell us what happens with it and what all those variables are
以前的评论和回答
Information_Schema
应注意仅适用于MySQL/Mariadb。诚然,这是您的dbi-> connect
语句指示您正在使用的数据库,但还有一种独立于数据库 - 引擎 - 引擎的方法:The previous comments and answer referring to
INFORMATION_SCHEMA
should be noted as only applicable to MySQL/MariaDB. Which, admittedly, is the database that yourDBI->connect
statement indicates you're using, but there is also a database-engine-independent way to do it:您可以在服务器上(包括系统数据库)中检索数据库的名称,
检索表的名称
可以在当前数据库中或在当前数据库中
You may retrieve the names of the databases which are present on the server (including system databases) with
You may retrieve the names of the tables in definite database with
or in current database with