我有这个光标
cursor.execute("SELECT price FROM Items WHERE itemID = (
SELECT item_id FROM Purchases
WHERE purchaseID = %d AND customer_id = %d)",
[self.purchaseID, self.customer])
我收到此错误
'Cursor' object has no attribute '_last_executed'
但是当我尝试这个时:
cursor.execute("SELECT price FROM Items WHERE itemID = (
SELECT item_id FROM Purchases
WHERE purchaseID = 1 AND customer_id = 1)",
)
没有错误。我该如何解决这个问题?
答案 0 :(得分:8)
我也遇到过这个问题。我将%d更改为%s,它就解决了。希望这对你有用。
答案 1 :(得分:5)
问题是你没有在你的选择字符串中正确地进行替换。来自docs:
def execute(self, query, args=None):
"""Execute a query.
query -- string, query to execute on server
args -- optional sequence or mapping, parameters to use with query.
Note: If args is a sequence, then %s must be used as the
parameter placeholder in the query. If a mapping is used,
%(key)s must be used as the placeholder.
Returns long integer rows affected, if any
"""
所以,它应该是:
cursor.execute("SELECT price FROM Items WHERE itemID = (
SELECT item_id FROM Purchases
WHERE purchaseID = ? AND customer_id = ?)",
(self.purchaseID, self.customer))
答案 2 :(得分:2)
原因是你正在使用'%d'。在SQL中使用'%'时,execute会将'%'解释为格式。你应该这样写下你的陈述:
cursor.execute("SELECT price FROM Items WHERE itemID = (
SELECT item_id FROM Purchases
WHERE purchaseID = %%d AND customer_id = %%d)",
[self.purchaseID, self.customer])
答案 3 :(得分:1)
使用double %%
为我工作 "SELECT title, address from table t1, table t2 on t1.id=t2.id where t1.title like '%%Brink%%' "
答案 4 :(得分:0)
根据您的SQL模块,您可能需要改用cursor.statement
。
答案 5 :(得分:0)
options={{
上面的代码应该显示对请求执行的所有请求。