[英]Perl and DBI - Load array Problem
I am new to Perl and need some help. 我是Perl的新手,需要帮助。
In Mysql I have a table with a todo-List filled up. 在Mysql中,我有一张桌子,上面有一个待办事项列表。
At the beginning of my script, I want to add these values to "my %todo" 在脚本的开头,我想将这些值添加到“我的%todo”中
But I can't figure out how to do this... 但是我不知道该怎么做...
Any idea? 任何想法?
OK, let's play martian rover though I'd rather see the code. OK,让我们玩火星漫游者,尽管我宁愿看到代码。
Do you use warnings; use strict
您是否use warnings; use strict
use warnings; use strict
? use warnings; use strict
吗? If not, do it. 如果不是,请执行此操作。 If yes, are there any warnings or errors? 如果是,是否有任何警告或错误?
If you put a print "while\\n";
如果在print "while\\n";
into your while loop, how many while
's will you get on screen? 进入while循环,您会在屏幕上看到多少while
? How many records are there in the table? 表格中有几条记录?
If you use DBI, turn on exceptions: $dbh->RaiseError(1);
如果使用DBI,请打开以下异常: $dbh->RaiseError(1);
($dbh is you database handle here) before any operations with DB. (对数据库执行任何操作之前,$ dbh是您的数据库句柄)。
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. 我不明白为什么要求“加载数组”并指定哈希%todo,但是如果您想一次将表读入内存,则应该查看$ dbh-> selectall_arrayref()方法。
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();
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.