本周我第一次使用 perl DBI。
大多数查询/插入都可以正常工作,但是我遇到了一个返回 0 行的特定查询的问题。当我为 perl DBI 启用跟踪,并将完全相同的语句从跟踪复制到服务器(通过 HeidiSQL)时,返回 1 行。
原始 SQL 查询是否存在歧义?目的是检索具有最新时间戳的行。时间戳列中没有重复项。
数据库连接的初始设置:
$dsn = 'dbi:mysql:<servername>:<port>';
$dbh = DBI->connect($dsn, "<username>","<password>") or die "unable to connect $DBI::errstr\n";
准备和执行语句:代码到达 print 'no rows found'
my $sth = $dbh->prepare("SELECT name, location, timestamp, notified FROM storage
WHERE name = ? AND location = ?
AND timestamp = (SELECT MAX(timestamp) FROM storage)");
$sth->execute($strg_data->{name}, $strg_data->{location});
my @latest = $sth->fetchrow_array();
if (@latest) {
<snipped>
}
else {
print "no rows found!\n";
}
从 perl DBI 跟踪中提取(级别设置为 2):
-> prepare for DBD::mysql::db (DBI::db=HASH(0xebe4c0)~0xec0010 'SELECT name, location, timestamp, notified FROM storage
WHERE name = ? AND location= ? AND timestamp = (SELECT MAX(timestamp) FROM storage)')
Setting mysql_use_result to 0
<- prepare= DBI::st=HASH(0xecd7d0) at monitor.pl line 147
-> execute for DBD::mysql::st (DBI::st=HASH(0xecd7d0)~0xec9e50 'xxxx' '/tmp/')
-> dbd_st_execute for 00ecd7a0
-> mysql_st_interal_execute
Binding parameters: SELECT name, location, timestamp, notified FROM storage
WHERE name = 'xxxx' AND location= '/tmp/' AND timestamp = (SELECT MAX(timestamp) FROM storage)
<- mysql_st_internal_execute returning rows 0
<- dbd_st_execute returning imp_sth->row_num 0
<- execute= '0E0' at monitor.pl line 152