Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在perl脚本中从文本文件调用DBI connect参数?_Perl_File - Fatal编程技术网

如何在perl脚本中从文本文件调用DBI connect参数?

如何在perl脚本中从文本文件调用DBI connect参数?,perl,file,Perl,File,我有一个代码,其中我从表中提取数据并显示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';

我有一个代码,其中我从表中提取数据并显示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文件并在其中输入凭据,然后使用从中提取值

然后,您可以将这些值传递给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);
然后,您可以编写一个名为get_credentials的子例程,如下所示:

Oracle:DB_INST:USER:PASS
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";
关于代码的其他一些注释

始终在代码中包含“使用严格”和“使用警告”。 为什么要使用Shell?我想你不会用它吧? 您创建了两个CGI对象$CGI和$CGI\u ST。您只需要一个。 从上个千年开始,我们就知道在Perl代码中放入原始HTML是一个糟糕的想法。看看如何使用模板将HTML与Perl分开。 打印两个CGI标题打印$CGI->标题和打印内容类型:text/html\n\n。那太多了。 始终检查打开文件、header.asp或die中的返回值。。。 使用三个arg版本的open和lexicalfilehandles打开我的$fh,'start_table,而不是$cgi->table。但是这个。请不要使用它们。
最后,也是最重要的一点,CGI是一种古老的技术。请阅读并切换到基于PSGI的现代技术。

您是在问如何从文本文件中读取行吗?你的代码已经做到了!打开文件header.asp;我的$file=;关闭文件;我想从输入文件调用-'DB_INST',USER,PASS。值将位于第行下方$dbh=DBI->connect'DBI:Oracle:DB_INST',未建立用户、过程或死亡数据库连接:$DBI::errstr;