无法在传递参数后连接到数据库。出现“无效的参数值”错误。

huangapple go评论54阅读模式
英文:

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{&#39;db2_user&#39;};

  my $name = $data{&#39;db2_name&#39;};

  my $password= $data{&#39;db2_passwd&#39;};

  print $user. &quot;\n&quot;. $name &quot;\n&quot;. $password. &quot;\n&quot;;

  my $dbh = DBI-&gt;connect(&quot;DBI: DB2: $name&quot;, &quot;$user&quot;, &quot;$password&quot;) 
    or die (&quot;Cannot connect to DB&quot;);

}

# 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 &quot;Usage

  The script reads two &lt;config file&gt;&quot;;
}

open (my $input_fh, &quot;&lt;&quot;, $details_file)
  or die &quot;Cannot open config file: $!&quot;;

my %data;

while (my $line = &lt;$input_fh&gt;) {
  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-&gt;connect(&quot;dbi:DB2:db_name&quot;, $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-&gt;connect(&quot;dbi:DB2:$name&quot;, $user, $password)
  or die (&quot;Cannot connect to DB&quot;);

答案2

得分: 0

$line =~ /^(\w+)\s*=\s*(.*)$/
此正则表达式只匹配类似
'foo *=bar'
的行,这可能不是你想要的。
你已经转义了星号,这意味着它会匹配字面的星号。
你可能想要的是 `/^(\w+)\s*=\s*(.*)$/`。
英文:
$line =~  /^(\w+)\s\*=\s*(.*)$/

This regular expression only matches lines like

&#39;foo *=bar&#39;

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.

huangapple
  • 本文由 发表于 2023年3月12日 15:24:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定