11

我有这个光标

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)", 
                  )

没有错误。我该如何解决?

4

6 回答 6

8

我也遇到了这个问题。我把%d改成%s,就解决了。希望这对你有用。

于 2013-01-23T07:01:46.677 回答
5

问题是您没有在选择字符串中正确进行替换。来自文档:

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))
于 2012-10-02T14:20:34.150 回答
2

原因是您使用的是“%d”。当您在 SQL 中使用 '%' 时,执行会将 '%' 解释为格式。你应该这样写你的陈述:

cursor.execute("SELECT price FROM Items WHERE itemID = ( 
                SELECT item_id FROM Purchases 
                WHERE purchaseID = %%d AND customer_id = %%d)", 
                [self.purchaseID, self.customer])
于 2013-02-26T13:51:08.493 回答
1

根据您的 SQL 包,您可能需要cursor.statement改用。

于 2020-05-25T04:08:32.573 回答
1

使用 double %% 为我工作

  "SELECT  title, address from table t1, table t2 on t1.id=t2.id where t1.title like '%%Brink%%' "
于 2015-10-24T17:35:36.883 回答
0
from django.db import connection
print(connection.queries)

上面的代码应该显示对请求执行的所有请求。

于 2021-02-23T18:03:37.380 回答