[英]Inserting an array with single quotes(') in database with 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.