我有一个代码,其中我从表中提取数据并显示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;
答案 0 :(得分:1)
您希望从属性文件中解析数据库凭据,而不是对其进行硬编码。创建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
答案 1 :(得分:0)
创建一个文件database.conf,您可以使用任何格式的CSV,XLS或简单分隔的Flat文件来编写它,以便您可以轻松地解析它。我正在考虑一种格式,我使用'='作为分隔符。
# 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";
..............
答案 2 :(得分:0)
您已经拥有从文件中读取数据的代码:
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()
的三个arg版本和词法文件句柄(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 generation functions have been deprecated。请不要使用它们。最后,最重要的是,CGI是旧技术。请阅读CGI::Alternatives并切换到基于PSGI的现代技术。