[英]How to call DBI connect parameters from text file in perl script?
I'm having a code in which i'm extracting data from table and displaying the O/P.我有一个代码,我在其中从表中提取数据并显示 O/P。 But i want to give connection string from txt file(not hardcoded in script) .
但我想从 txt 文件中给出连接字符串(不是硬编码在脚本中)。 Can anyone help how i can provide below values from input file.
任何人都可以帮助我如何从输入文件中提供以下值。
$dbh = DBI->connect( 'dbi:Oracle:DB_INST',"USER","PASS")
or die "Database connection not made: $DBI::errstr";
My code:我的代码:
#!/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;
You want to parse database credentials from properties file instead of hard coding it.您想从属性文件中解析数据库凭据,而不是对其进行硬编码。 Create
ini
file and enter credentials into them and then use Config::IniFiles
to extract value out of them.创建
ini
文件并在其中输入凭据,然后使用Config::IniFiles
从中提取值。
Then, You can pass those values to DBI
and connect to database.然后,您可以将这些值传递给
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";
Here is config file:这是配置文件:
[DATABASE]
# DB string
DBINST=XXX
# database username
DBUSER=XXX
# database password
DBPASS=XXX
Create a file database.conf and you can write it in any format CSV, XLS or simple delimited Flat file so that you can easily parse it.创建一个文件database.conf,您可以将其写入任何格式的CSV、XLS 或简单的分隔平面文件,以便您可以轻松解析它。 I'm considering a format where I'm using '=' as a separator.
我正在考虑使用 '=' 作为分隔符的格式。
# filename database.conf
HOST=dbi:Oracle:DB_INST
USER=USER,
PASSWORD=PASS
Now in your perl script use following code for retrieving it:现在在您的 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";
..............
You already have code that reads data from a file:您已经有了从文件中读取数据的代码:
open (FILE,"header.asp");
my $file = <FILE>;
close(FILE);
So I'm not really sure what problem you are having.所以我不确定你遇到了什么问题。
Let's assume that you put the details in a file called credentials.txt which looks like this like this:假设您将详细信息放在一个名为 credentials.txt 的文件中,该文件如下所示:
Oracle:DB_INST:USER:PASS
You could then write a subroutine called get_credentials()
which looks like this:然后,您可以编写一个名为
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;
}
You would then call it like this:然后你会这样称呼它:
my ($type, $instance, $user, $pass) = get_credentials();
my $dbh = DBI->connect( "dbi:$type:$instance", $user, $pass)
or die "Database connection not made: $DBI::errstr";
A few other comments on your code.关于您的代码的其他一些评论。
use strict
and use warnings
in your code.use strict
和use warnings
。use Shell
?use Shell
? I don't think you use it?$cgi
and $CGI_ST
).$cgi
和$CGI_ST
)。 You only need one.print $cgi->header
and print "Content-type: text/html\\n\\n"
. That's one too many.print $cgi->header
并print "Content-type: text/html\\n\\n"
。这太多了。open()
( open (FILE,"header.asp") or die ...
)open()
的返回值( open (FILE,"header.asp") or die ...
)open()
and lexical filehandles ( open (my $fh, '<', 'header.asp') or die ...
).open()
和词法文件句柄的三参数版本( open (my $fh, '<', 'header.asp') or die ...
)。print "$file"
is better written print $file
and if ( "$product" eq "2" && "$environment" eq "MPET" )
is better written if ( $product eq "2" && $environment eq "MPET" )
.print "$file"
最好写成print $file
并且if ( "$product" eq "2" && "$environment" eq "MPET" )
最好写if ( $product eq "2" && $environment eq "MPET" )
。$cgi->start_table()
, not $cgi->table()
.$cgi->start_table()
,而不是$cgi->table()
。 But the HTML generation functions have been deprecated . Finally, and most importantly, CGI is old technology.最后,也是最重要的,CGI 是一种古老的技术。 Please read CGI::Alternatives and switch to a modern technology that is based on PSGI.
请阅读CGI::Alternatives并切换到基于 PSGI 的现代技术。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.