[英]Perl - update mysql table without primary key
我有一张没有主键的表。 我需要执行以下操作:
UPDATE t1
SET tstamp = now()
WHERE `col1` = 1
AND `col2` = 'this';
在 Workbench 中,它会引发Error 1175
直到我在更新之前执行此行:
SET SQL_SAFE_UPDATES = 0;
有了这条线,它工作得很好。
但是当我尝试在 perl 中执行此操作时,它不起作用。 我都试过了
$dbh->do("SET SQL_SAFE_UPDATES = 0");
和
my $dbh = DBI->connect("DBI:mysql:$db:$host:$port",$user,$password, { RaiseError => 1, AutoCommit => 0, sql_safe_updates => 0 })
但它仍然不起作用。
我怎样才能在 perl 中完成这项工作?
更新。 我用@@sql_safe_updates 检查和提交更新了代码。
编码:
$sth = $dbh->prepare("SELECT @\@sql_safe_updates"); $sth->execute; while(my @row = $sth->fetchrow_array) { print "sql_safe_updates before: ". $row[0] . "\n"; }
$dbh->do("SET SQL_SAFE_UPDATES = 0") or die $dbh->errstr;
$sth = $dbh->prepare("SELECT @\@sql_safe_updates"); $sth->execute; while(my @row = $sth->fetchrow_array) { print "sql_safe_updates after: " . $row[0] . "\n"; }
$query = "UPDATE t1 SET tstamp = now() WHERE `col1` = 1 AND `col2` = 'this'";
$sth = $dbh->prepare($query);
$rv = $sth->execute or die $sth->err();
$dbh->commit;
if ("$rv" ne "1") {
$query =~ s/\n/ /g; $query =~ s/ / /g;
print "Failed to run query: $query\n";
exit;
}
输出:
sql_safe_updates before: 0
sql_safe_updates after: 0
Failed to run query: UPDATE t1 SET tstamp = now() WHERE `col1` = 1 AND `col2` = 'this'
UPD2。 我检查了表格 - 在我提交后一切正常。 令人困惑的想法是,成功select
$rv 为 1,成功update
2
这是一个返回码检查错误和一个丢失的提交。
关于返回码检查,对于非SELECT
语句, execute
返回受影响的行数(如果已知)。 . 受影响的零行(与错误相反)表示为特殊的“零但为真”值“0E0”。 在 OP 的情况下,该语句返回 2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.