2015-09-28 54 views
2

我有一个没有主键的表。 我需要执行以下操作:Perl - 更新无主键的mysql表

UPDATE t1 
    SET tstamp = now() 
WHERE `col1` = 1 
    AND `col2` = 'this'; 

在工作台,它抛出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中获得此项工作?

UPD。 我使用@@ 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。我检查了桌子 - 在我提交之后一切都正常。这是令人困惑的想法,$ rv是1成功select和2成功update

+1

您'do'查询未被引用。这是否将它转化为问题或实际的语法错误? – simbabque

+0

@simbabque不,它只是在这里。它在脚本中被引用。我不会遇到任何语法错误,并严格遵守和警告。固定。 – Alexander

+0

它说什么吗?任何错误? – simbabque

回答