Perl 和 DBI - 加载数组问题

发布于 2024-10-27 09:56:51 字数 158 浏览 4 评论 0原文

我是 Perl 新手,需要一些帮助。

在 Mysql 中,我有一个表,其中填满了待办事项列表。

在脚本的开头,我想将这些值添加到 "my %todo"

但我不知道如何做到这一点......

有什么想法吗?

I am new to Perl and need some help.

In Mysql I have a table with a todo-List filled up.

At the beginning of my script, I want to add these values to "my %todo"

But I can't figure out how to do this...

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以笙箫默 2024-11-03 09:56:51

好吧,让我们玩一下火星车吧,尽管我更想看代码。

使用警告吗?使用严格的?如果没有,那就去做吧。如果是,是否有任何警告或错误?

如果将 print "while\n"; 放入 while 循环中,屏幕上会显示多少个 while ?表中有多少条记录?

如果您使用 DBI,请在对 DB 进行任何操作之前打开异常:$dbh->RaiseError(1);($dbh 是您的数据库句柄)。

OK, let's play martian rover though I'd rather see the code.

Do you use warnings; use strict? If not, do it. If yes, are there any warnings or errors?

If you put a print "while\n"; into your while loop, how many while's will you get on screen? How many records are there in the table?

If you use DBI, turn on exceptions: $dbh->RaiseError(1); ($dbh is you database handle here) before any operations with DB.

深巷少女 2024-11-03 09:56:51

我不明白为什么你要求“加载数组”并指定散列%todo,但如果你想将表读入内存一次,你应该查看 $dbh->selectall_arrayref() 方法。

补充:看看这是否能让你开始:

    my $dsn = '...';
    my $user = '...';
    my $password = '...';
    my $dbh = DBI->connect( $dsn, $user, $password, { RaiseError => 1, AutoCommit => 0 } );
    my $sql = 'SELECT ... FROM Todo';
    my %todo = ();
  if (0) {
    my $sth = $dbh->prepare( $sql );
    $sth->execute();
    while (my $aref = $sth->fetchrow_arrayref()) {
      $todo{ $aref->[ 0 ] } = $aref->[ 1 ];
    }
    $sth->finish();
  } else {
    my $aref = $dbh->selectall_arrayref($sql);
    for (@$aref) {
      $todo{ $_->[ 0 ] } = $_->[ 1 ];
    }
  }
    for (keys( %todo )) {
      print $_, "\n", $todo{ $_ }, "\n\n";
    }
    my $rc = $dbh->disconnect();

I don't understand why you ask for "load array" and specify a hash %todo, but if you want to read a table into memory once, you should look at the $dbh->selectall_arrayref() method.

Added: See if this get you started:

    my $dsn = '...';
    my $user = '...';
    my $password = '...';
    my $dbh = DBI->connect( $dsn, $user, $password, { RaiseError => 1, AutoCommit => 0 } );
    my $sql = 'SELECT ... FROM Todo';
    my %todo = ();
  if (0) {
    my $sth = $dbh->prepare( $sql );
    $sth->execute();
    while (my $aref = $sth->fetchrow_arrayref()) {
      $todo{ $aref->[ 0 ] } = $aref->[ 1 ];
    }
    $sth->finish();
  } else {
    my $aref = $dbh->selectall_arrayref($sql);
    for (@$aref) {
      $todo{ $_->[ 0 ] } = $_->[ 1 ];
    }
  }
    for (keys( %todo )) {
      print $_, "\n", $todo{ $_ }, "\n\n";
    }
    my $rc = $dbh->disconnect();
堇色安年 2024-11-03 09:56:51
use strict;
use warnings;
my $dbh = $dbh->connect;
$dbh->{RaiseError} = 1;
my $sth = $dbh->prepare(q/select id, to_do from to_do_table/);
$sth->execute;
my %todo;
while(my ($id, $to_do) = $sth->fetchrow) {
    $todo{$index_column} = $to_do;
}
$dbh->disconnect;
use strict;
use warnings;
my $dbh = $dbh->connect;
$dbh->{RaiseError} = 1;
my $sth = $dbh->prepare(q/select id, to_do from to_do_table/);
$sth->execute;
my %todo;
while(my ($id, $to_do) = $sth->fetchrow) {
    $todo{$index_column} = $to_do;
}
$dbh->disconnect;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文