[英]How to call DBI connect parameters from text file in perl script?
我有一個代碼,我在其中從表中提取數據並顯示 O/P。 但我想從 txt 文件中給出連接字符串(不是硬編碼在腳本中)。 任何人都可以幫助我如何從輸入文件中提供以下值。
$dbh = DBI->connect( 'dbi:Oracle:DB_INST',"USER","PASS")
or die "Database connection not made: $DBI::errstr";
我的代碼:
#!/usr/bin/perl
$ENV{ORACLE_HOME}='/ora/11.2.0.3';
$LD_LIBRARY_PATH='/ora/11.2.0.3/lib';
use Shell;
use DBI ;
use CGI ;
my $cgi = new CGI;
print $cgi->header;
my $dbh;
$CGI_ST = new CGI;
#################### FUNCTIONS DECLARATION ########################################################
sub Start_HTM
{
print "<html>\n\n";
print "<title>LOGICAL DATE CHECK</title>\n\n";
print "<body>\n<center>\n";
print "<hr><h1 align=center><font color=#FFA500><u>LOGICAL DATE CHECK</u></font></h1>\n";
}
sub End_HTM
{
print "<a href=\"#\" onClick=window.close()>Close Window</a></b></small>";
print "</center>\n</body>\n</html>";
}
sub DisBackButton
{
print "<br><br><br><INPUT TYPE=button value=Back onClick=history.back()>";
}
####################################################################################################
print "Content-type: text/html\n\n";
open (FILE,"header.asp");
my $file = <FILE>;
close(FILE);
print "$file";
print "<SCRIPT LANGUAGE=JavaScript>";
print "</script>";
my $environment=$CGI_ST->param("environment");
my $product=$CGI_ST->param("product");
Start_HTM();
if ( "$product" eq "2" && "$environment" eq "MPET" ) {
print $cgi->start_html(-title=>'Basic CGI');
# you should change the connect method call to use the DBD you are
# using. The following examples all use DBD::ODBC and hence the
# first argument to the connect method is 'dbi:ODBC:DSN'.
my $dsn = "DBI:Oracle:$db_inst";
$dbh = DBI->connect( 'dbi:Oracle:DB_INST',"USER","PASS") or die "Database connection not made: $DBI::errstr";
my $sql = qq{SELECT logical_date,logical_date_type from logical_date where expiration_date is null };
my $sth = $dbh->prepare( $sql ) || die $dbh->errstr;
$sth->execute() || die $dbh->errstr;
print $cgi->table( { -border=>"1" } );
while (@data = $sth->fetchrow_array()) {
$Logical_Date_O = $data[0];
$Logical_Date_B = $data[1];
$Logical_Date_R = $data[2];
print "<tr><td><font color='black'>$Logical_Date_O</font></td>
<td>$Logical_Date_B</td><td>$Logical_Date_R</td></tr>\n";
}
}
print $cgi->end_table;
print $cgi->end_html;
您想從屬性文件中解析數據庫憑據,而不是對其進行硬編碼。 創建ini
文件並在其中輸入憑據,然后使用Config::IniFiles
從中提取值。
然后,您可以將這些值傳遞給DBI
並連接到數據庫。
my $cfg = Config::IniFiles -> new( -file => 'path/config/database_config.ini' );
my $dbinst = $cfg -> val( 'DATABASE', 'DBINST' );
my $dbuser = $cfg -> val( 'DATABASE', 'DBUSER' );
my $dbpass = $cfg -> val( 'DATABASE', 'DBPASS' );
my $dbh = DBI->connect( "dbi:Oracle:$dbinst", $dbuser, $dbpass)
or die "Database connection not made: $DBI::errstr";
這是配置文件:
[DATABASE]
# DB string
DBINST=XXX
# database username
DBUSER=XXX
# database password
DBPASS=XXX
創建一個文件database.conf,您可以將其寫入任何格式的CSV、XLS 或簡單的分隔平面文件,以便您可以輕松解析它。 我正在考慮使用 '=' 作為分隔符的格式。
# filename database.conf
HOST=dbi:Oracle:DB_INST
USER=USER,
PASSWORD=PASS
現在在您的 perl 腳本中使用以下代碼來檢索它:
my $db_conf_file = 'database.conf';
my $delimiter = '='; # You can change the delimiter accordingly
my %db;
open(my $FH, '<', $db_conf_file) or die "Could not open $db_conf_file";
while(my $line = <$FH>){
chomp $line;
my @fields = split $delimiter , $line;
$db->{$fields[0]} = $fields[1];
}
close($FH);
$dbh = DBI->connect($db->{HOST}, $db->{USER},$DB->{PASSWORD})
or die "Database connection not made: $DBI::errstr";
..............
您已經有了從文件中讀取數據的代碼:
open (FILE,"header.asp");
my $file = <FILE>;
close(FILE);
所以我不確定你遇到了什么問題。
假設您將詳細信息放在一個名為 credentials.txt 的文件中,該文件如下所示:
Oracle:DB_INST:USER:PASS
然后,您可以編寫一個名為get_credentials()
的子例程,如下所示:
sub get_credentials {
my $cred_file = 'credentials.txt';
open my $cred_fh, '<', $cred_file
or die "Can't open $cred_file: $!\n"
my $data = <$cred_fh>;
chomp $data;
return split /:/, $data;
}
然后你會這樣稱呼它:
my ($type, $instance, $user, $pass) = get_credentials();
my $dbh = DBI->connect( "dbi:$type:$instance", $user, $pass)
or die "Database connection not made: $DBI::errstr";
關於您的代碼的其他一些評論。
use strict
和use warnings
。use Shell
? 我不認為你使用它?$cgi
和$CGI_ST
)。 你只需要一個。print $cgi->header
並print "Content-type: text/html\\n\\n"
。這太多了。open()
的返回值( open (FILE,"header.asp") or die ...
)open()
和詞法文件句柄的三參數版本( open (my $fh, '<', 'header.asp') or die ...
)。print "$file"
最好寫成print $file
並且if ( "$product" eq "2" && "$environment" eq "MPET" )
最好寫if ( $product eq "2" && $environment eq "MPET" )
。$cgi->start_table()
,而不是$cgi->table()
。 但是HTML 生成功能已被棄用。 請不要使用它們。最后,也是最重要的,CGI 是一種古老的技術。 請閱讀CGI::Alternatives並切換到基於 PSGI 的現代技術。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.