I'm trying to pull data from 2 tables and then insert result into a third table. My code follows but only does 1 correct entry, and the rest are blank. There are 348 entries total. What am I missing here?
$dbh = DBI->connect(
"DBI:mysql:$mysqldatabase:$mysqlhostname",
"$mysqlusername",
"$mysqlpassword"
);
if(!$dbh) { die("Error: could not get DBI handle\n"); }
$sqlConnect = 1;
$SQL =<<SQL;
SELECT * FROM oscmax2.info2
SQL
$sth = $dbh->prepare($SQL);
if(!$sth) { die("Error: " . $dbh->errstr . "\n"); }
if(!$sth->execute) { die("Error4: " . $sth->errstr . "\n"); }
while (my @row = $sth->fetchrow_array) {
$products_id = $FORM{'product_id'};
$affiliate_id = $FORM{'affiliate_id'};
$demo = $FORM{'demo'};
}
if($sth->rows != 0) {
$total_rows = $sth->rows;
for ($counter = 0; $counter < $total_rows; $counter++) {
$SQL =<<SQL;
SELECT products_attributes_id FROM oscmax2.products_attributes
WHERE products_id = '$products_id'
SQL
$sth = $dbh->prepare($SQL);
if(!$sth) { die("Error: " . $dbh->errstr . "\n"); }
if(!$sth->execute) { die("Error: " . $sth->errstr . "\n");}
while (my @row = $sth->fetchrow_array) {
$products_attributes_id = $FORM2{'products_attributes_id'};
}
$SQL =<<SQL;
INSERT INTO oscmax2.products_attributes_download(
products_attributes_id, products_attributes_filename,
products_attributes_maxdays, products_attributes_maxcount
)
VALUES
('$products_attributes_id', '$affiliate_id/$demo', '7', '1')
SQL
$dbh->do($SQL) || die("Error5: " . $dbh->errstr . "\n");
}
}
$sth->finish();
if($sqlConnect) { $dbh->disconnect();
答案 0 :(得分:2)
块
while (my @row = $sth->fetchrow_array) {
$products_id = $FORM{'product_id'};
$affiliate_id = $FORM{'affiliate_id'};
$demo = $FORM{'demo'};
}
和
while (my @row = $sth->fetchrow_array) {
$products_attributes_id = $FORM2{'products_attributes_id'};
}
错了。您使用@row
接受结果每行的数据,但从不使用它。数据库提取不会影响%FORM
和%FORM2
,因此您只需多次从中收集相同的数据
猜测你想要这样的东西。请学习并使用这些技术,而不是按原样复制和测试,因为我无法知道数据库的结构是什么,我做了几个猜测
您应该注意以下几点
如果失败,则无需测试每个DBI操作的状态和die
。默认情况下,PrintError
选项已启用,如果有任何错误,DBI将发出警告。如果您希望自己的计划改为die
,这是明智的,那么您可以启用RaiseError
并停用PrintError
,而DBI将为您完成所有操作
无需将表中的所有数据提取到内存中(我认为这是您尝试对while
循环执行的操作。您应该获取每一行< / em>进入一个数组并逐行处理数据,除非你有理由不这样做
您总是 prepare
您的陈述和使用占位符。然后,您可以将实际参数传递给execute
调用,DBI将为您正确引用它们。此外,您可以将所有prepare
个调用移到程序顶部,使您的逻辑更清晰易读
几乎没有理由致电finish
或disconnect
。当您的数据库或语句处理超出范围或程序结束时,Perl将为您做正确的事
我已将语句命名为句柄$select1
和$select2
。这些名字非常糟糕,但我不知道你的数据库的结构,所以我不能写得更好。那个shouyldn阻止你改进它们
我不得不猜测第一个SELECT语句返回的列。如果这三个变量与@row
的前三个元素不对应,那么您需要更正
您应避免在Perl词法标识符中使用大写字母。它们保留给像包名这样的全局字符,如果你不遵守这条规则,可能会导致令人讨厌的冲突
use strict;
use warnings;
my ($mysqldatabase, $mysqlhostname, $mysqlusername, $mysqlpassword) = qw/ dbase host user pass /;
my $dbh = DBI->connect(
"DBI:mysql:$mysqldatabase:$mysqlhostname",
"$mysqlusername",
"$mysqlpassword",
{RaiseError => 1, PrintError => 0}
) or die "Unable to connect to database: $DBI::errstr";
my $select1 = $dbh->prepare('SELECT * FROM oscmax2.info2');
my $select2 = $dbh->prepare(<<__END_SQL__);
SELECT products_attributes_id FROM oscmax2.products_attributes\
WHERE products_id = ?
__END_SQL__
my $insert = $dbh->prepare(<<__END_SQL__);
INSERT INTO oscmax2.products_attributes_download (
products_attributes_id,
products_attributes_filename,
products_attributes_maxdays,
products_attributes_maxcount
)
VALUES ( ?, ?, ?, ? )
__END_SQL__
$select1->execute;
while ( my @row = $select1->fetchrow_array ) {
my ($products_id, $affiliate_id, $demo) = @row;
$select2->execute($products_id);
while ( my @row = $select2->fetchrow_array ) {
my ($products_attributes_id) = @row;
$insert->execute($products_attributes_id, "$affiliate_id/$demo", 7, 1 );
}
}