[英]How to connect to a database in Perl
I am a newbie in Perl and Eclipse and i am trying to connect my Perl script to my database but it gives me an ERROR: "FILE DOES NOT EXIST" 我是Perl和Eclipse的新手,我正在尝试将Perl脚本连接到数据库,但它给我一个错误:“文件不存在”
#!/usr/bin/perl
use XML::Simple;
use DBI;
use strict;
use Data::Dumper;
use constant {false=>0,true=>1};
my ( $exec_dir, $exec_file ) =
( Win32::GetFullPathName($0) =~ /^(.*)\\([^\\]*)$/ );
my $parmfil = $exec_dir . "\\parms\\parms.xml";
my $p_ref = XMLin($parmfil);
my ($dsn,$dbusr,$dbpwd) = ( $p_ref->{"DSN"}
,$p_ref->{"db_user"}
,$p_ref->{"db_pwd"}
);
my $dbh = DBI->connect( "dbi:ODBC:$dsn", $dbusr, $dbpwd )
or die "SDM Database connect error - $DBI::errstr\n";
$dbh->{LongReadLen} = 1024 * 1024;
if (true)
{
print "true\n";
}
Please Help me out. 请帮帮我。
I am more inclined to trust Perl that the file doesn't exist than your assurances that it does ! 我更倾向于信任的Perl文件不超过你的存在保证它呢 !
You should print the value of $parmfil
and see if it's what you expect it to be. 您应该打印
$parmfil
的值,看看它是否是您期望的值。
You can also add this 您也可以添加此
print "\$parmfil %s exist\n", -f $parmfil ? 'DOES' : 'DOESN'T';
to get a "second opinion". 获得“第二意见”。
This dosn't look right 这看起来不对
my ( $exec_dir, $exec_file ) =
( Win32::GetFullPathName($0) =~ /^(.*)\\([^\\]*)$/ );
GetFullPathName in list contect will return pathname and filename as you require, but the reguler expression match will change that, can't see that its needed. 列表连接中的GetFullPathName将根据您的需要返回路径名和文件名,但是常规表达式匹配将改变它,看不到它所需要的。 If you want to manipulate the path then think its best to do so after the values have been returned.
如果要操作路径,请在返回值后最好进行操作。
Try this code: 试试这个代码:
use strict;
use FindBin qw($Bin);
my $parmfil = $Bin . "/parms/parms.xml";
my $p_ref = XMLin($parmfil);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.