英文:
Unable to connect to db after passing arguments. Getting error as Invalid argument values
问题
在编译后,我得到了以下错误。在打印用户、名称和密码详细信息之后,出现了以下错误。
失败:无效的参数值。无法连接到数据库。
即使传递了正确的参数值,它也只接受空格,而不是传递的详细信息。
#!/usr/bin/perl -w
use warnings;
use strict;
use DBI;
sub routine {
my (%data) = @_;
my $user = $data{'db2_user'};
my $name = $data{'db2_name'};
my $password= $data{'db2_passwd'};
print $user. "\n". $name. "\n". $password. "\n";
my $dbh = DBI->connect("DBI:DB2:$name", "$user", "$password")
or die("无法连接到数据库");
}
# 主函数
my $config_file;
my $details_file;
# 检查命令行参数的数量
if (@ARGV == 2) {
$config_file=$ARGV[0];
$details_file=$ARGV[1];
}
else {
die "用法
脚本读取两个<配置文件>";
}
open (my $input_fh, "<", $details_file)
or die "无法打开配置文件: $!";
my %data;
while (my $line = <$input_fh>) {
chomp $line;
if ($line =~ /^(\w+)\s\*=\s*(.*)$/) {
$data{$1} = $2;
}
}
close $input_fh;
routine(%data);
exit 0;
英文:
After compilation I am getting error as . It is printing user , name and password detail after that getting error as.
failed: invalid argument value. Cannot connect to db.
Even after passing the correct argument value it is taking only space and not the details which were passed.
#!/usr/bin/perl -w
use warnings;
use strict;
use DBI;
sub routine {
my (%data) = @_;
my $user = $data{'db2_user'};
my $name = $data{'db2_name'};
my $password= $data{'db2_passwd'};
print $user. "\n". $name "\n". $password. "\n";
my $dbh = DBI->connect("DBI: DB2: $name", "$user", "$password")
or die ("Cannot connect to DB");
}
# Main function
my $config_file;
my $details_file;
# Check number of argument on command line
if (@ARGV == 2) {
$config_file=$ARGV[0];
$details_file=$ARGV[1];
}
else {
die "Usage
The script reads two <config file>";
}
open (my $input_fh, "<", $details_file)
or die "Cannot open config file: $!";
my %data;
while (my $line = <$input_fh>) {
chomp $line;
if ($line =~ /^(\w+)\s\*=\s*(.*)$/) {
$data{$1} = $2;
}
}
close $input_fh;
routine (%data);
exit 0;
答案1
得分: 2
[DBD::DB2的文档](https://metacpan.org/dist/DBD-DB2/view/DB2.pod)显示连接字符串如下:
$dbh = DBI->connect("dbi:DB2:db_name", $username, $password);
有没有特定原因你改成了稍微不同的格式?按照你的代码转换,应该是:
my $dbh = DBI->connect("dbi:DB2:$name", $user, $password)
or die ("无法连接到数据库");
英文:
The documentation for DBD::DB2 shows the connection string like this:
$dbh = DBI->connect("dbi:DB2:db_name", $username, $password);
Is there any reason why you changed it to have a slightly different format? Converting it to match your code, it would be:
my $dbh = DBI->connect("dbi:DB2:$name", $user, $password)
or die ("Cannot connect to DB");
答案2
得分: 0
$line =~ /^(\w+)\s*=\s*(.*)$/
此正则表达式只匹配类似
'foo *=bar'
的行,这可能不是你想要的。
你已经转义了星号,这意味着它会匹配字面的星号。
你可能想要的是 `/^(\w+)\s*=\s*(.*)$/`。
英文:
$line =~ /^(\w+)\s\*=\s*(.*)$/
This regular expression only matches lines like
'foo *=bar'
which is probably not what you intended.
You have escaped the asterisk, which means it's looking for a literal asterisk.
You probably wanted /^(\w+)\s*=\s*(.*)$/
instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论