-1

这个没有错误

cursor.execute ("update `1` set `2`='aaa' where `1`='5'")

这个有错误。

cursor.execute ("update `1` set `2`=aaa where `1`='5'")

不同之处在于,我试图将变量“aaa”传递给它。

这是apache的error_log

[2013 年 5 月 14 日星期二 21:20:24] [错误] [客户端 127.0.0.1] OperationalError:(1054,“'字段列表'中的未知列'aaa'”)

在 PHP 中我只需输入$aaamysql 查询,因此假设在 python 中我只需输入aaa.

4

2 回答 2

3

在 php 中你可以做execute("Update '1' set '2'=$aaa where '1'='5'"),但你应该execute("Update '1' set '2'=? where '1'='5'", $aaa)pdo做。但你至少应该这样做execute("Update '1' set '2'=".mysql_real_escape_string($aaa)." where '1'='5'")

在 Python 中,您使用execute("Update '1' set '2'=%s where '1'='5'", [aaa]).

于 2013-05-15T04:43:46.923 回答
-2

如果你试图传递一个变量的值,你需要在字符串之外写上“aaa”。所以,而不是:

cursor.execute ("update `1` set `2`=aaa where `1`='5'")

尝试:

cursor.execute ("update `1` set `2`= '" + aaa + "' where `1`='5'")

请注意,如果您使用的是 python,您可能需要将变量转换为字符串(例如,如果它包含一个数字),使用:

str(aaa)
于 2013-05-15T04:34:49.060 回答