2015-09-28 51 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

回答