簡體   English   中英

Perl和DBI-加載數組問題

[英]Perl and DBI - Load array Problem

我是Perl的新手,需要幫助。

在Mysql中,我有一張桌子,上面有一個待辦事項列表。

在腳本的開頭,我想將這些值添加到“我的%todo”中

但是我不知道該怎么做...

任何想法?

OK,讓我們玩火星漫游者,盡管我寧願看到代碼。

您是否use warnings; use strict use warnings; use strict嗎? 如果不是,請執行此操作。 如果是,是否有任何警告或錯誤?

如果在print "while\\n"; 進入while循環,您會在屏幕上看到多少while 表格中有幾條記錄?

如果使用DBI,請打開以下異常: $dbh->RaiseError(1); (對數據庫執行任何操作之前,$ dbh是您的數據庫句柄)。

我不明白為什么要求“加載數組”並指定哈希%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();
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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM