如何获取数据库中存在的表名称

发布于 2025-02-02 13:37:28 字数 5289 浏览 3 评论 0原文

我有一个代码,我想获取数据库信息并存储所需的数据,我已经连接到数据库,我想要的是获取数据库中存在的表的名称,以便我可以访问它们一一获取所需的信息。

这是

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰之心 2025-02-09 13:37:28

使用$ dbcon-> table()在问题中尝试的确实是一种很好的方法,但是返回表格列表 - 虽然将其分配给标量,因此您可以获得表的数量,如所观察到的。,也将其称为无参数(请参见下面链接的文档)。 Dave Sherohman在答案中显示了一种正确的使用方法。

或者,请使用 table_info /metacpan.org/dist/dbi/view/dbi.pm#tables“ rel =” nofollow noreferrer“>表格是“ 简单接口”),以获取和查看更多详细信息不仅仅是名称,并查看哪个表适合您的目的

my $sth = $dbh->table_info('', '', '%', 'TABLE');
my $res = $sth->fetchall_arrayref;
print Dumper $res;

my $sth = $dbh->table_info('', '', '', 'TABLE');

或者甚至

my $sth = $dbh->table_info(undef, undef, undef, 'TABLE');

忽略了最后一个'table'(并使用'''或undef)然后,view s也将包含在resutls中。


这个问题也显示了正确

my @names = $dbcon->tables( $catalog, $schema, $table, $type );

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

my $sth = $dbh->table_info('', '', '%', 'TABLE');
my $res = $sth->fetchall_arrayref;
print Dumper $res;

or with

my $sth = $dbh->table_info('', '', '', 'TABLE');

or even

my $sth = $dbh->table_info(undef, undef, undef, 'TABLE');

If you leave out that last 'TABLE' (and use '' or undef instead) then the VIEWs will be included in the resutls as well, what may be of interest.


The question also shows the correct

my @names = $dbcon->tables( $catalog, $schema, $table, $type );

but it doesn't tell us what happens with it and what all those variables are

那伤。 2025-02-09 13:37:28

以前的评论和回答Information_Schema应注意仅适用于MySQL/Mariadb。诚然,这是您的dbi-> connect语句指示您正在使用的数据库,但还有一种独立于数据库 - 引擎 - 引擎的方法:

my @tables = $dbcon->tables(undef, undef, undef, 'TABLE');

The previous comments and answer referring to INFORMATION_SCHEMA should be noted as only applicable to MySQL/MariaDB. Which, admittedly, is the database that your DBI->connect statement indicates you're using, but there is also a database-engine-independent way to do it:

my @tables = $dbcon->tables(undef, undef, undef, 'TABLE');
葬シ愛 2025-02-09 13:37:28

如何获取未知数据库中存在的表的名称

您可以在服务器上(包括系统数据库)中检索数据库的名称,

SELECT SCHEMA_NAME AS `Database`
FROM INFORMATION_SCHEMA.SCHEMATA;

检索表的名称

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name';

可以在当前数据库中或在当前数据库中

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = DATABASE();

how to get the name of the tables present in unknown database

You may retrieve the names of the databases which are present on the server (including system databases) with

SELECT SCHEMA_NAME AS `Database`
FROM INFORMATION_SCHEMA.SCHEMATA;

You may retrieve the names of the tables in definite database with

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name';

or in current database with

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = DATABASE();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文