Perl - 在没有主键的情况下更新 mysql table
Perl - update mysql table without primary key
我有一个没有主键的 table。
我需要执行以下操作:
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 check and commit.
更新了代码
代码:
$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。我检查了 table - 提交后一切正常。令人困惑的是,$rv 在成功 select
时为 1,在成功 update
时为 2
这是一个 return 代码检查错误,并且缺少提交。
关于 return 代码检查,对于非 SELECT
语句,execute
returns the number of rows affected, if known.。受影响的零行(与错误相反)表示为特殊的 "zero but true" 值“0E0”。在 OP 的例子中,语句是 returning 2.
我有一个没有主键的 table。 我需要执行以下操作:
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 check and commit.
更新了代码代码:
$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。我检查了 table - 提交后一切正常。令人困惑的是,$rv 在成功 select
时为 1,在成功 update
这是一个 return 代码检查错误,并且缺少提交。
关于 return 代码检查,对于非 SELECT
语句,execute
returns the number of rows affected, if known.。受影响的零行(与错误相反)表示为特殊的 "zero but true" 值“0E0”。在 OP 的例子中,语句是 returning 2.