Error: Cursor' object has no attribute '_last_executed
Asked Answered
I

6

12

I have this cursor

cursor.execute("SELECT price FROM Items WHERE itemID = ( 
                  SELECT item_id FROM Purchases 
                  WHERE purchaseID = %d AND customer_id = %d)", 
                  [self.purchaseID, self.customer])

I get this error

'Cursor' object has no attribute '_last_executed'

But when I try this:

cursor.execute("SELECT price FROM Items WHERE itemID = ( 
                  SELECT item_id FROM Purchases 
                  WHERE purchaseID = 1 AND customer_id = 1)", 
                  )

there is no error. How do I fix this?

Intellectualism answered 2/10, 2012 at 14:16 Comment(2)
Did you finally get it working?Persson
I had the same error message, but using the ORM. It turned out to be charset problem solved with encapsulating my string with unicode(). I know it does not answer your question, but it might fit with others that land upon this page in search for answers.Googolplex
S
8

I encountered this problem too. I changed the %d to %s, and it is solved. Wish this is useful for you.

Stringer answered 23/1, 2013 at 7:1 Comment(1)
This worked for me. I guess you have to pass %s even for ints.Solenne
P
6

The problem is that you are not making substitutions properly in your select string. From 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

    """

So, it should be:

cursor.execute("SELECT price FROM Items WHERE itemID = ( 
              SELECT item_id FROM Purchases 
              WHERE purchaseID = ? AND customer_id = ?)", 
              (self.purchaseID, self.customer))
Persson answered 2/10, 2012 at 14:20 Comment(1)
what version of python are you using?Persson
C
2

The reason is that you are using '%d'. When you use '%' in SQL, the execute will interpret the '%' as the format. You should write your statement like this:

cursor.execute("SELECT price FROM Items WHERE itemID = ( 
                SELECT item_id FROM Purchases 
                WHERE purchaseID = %%d AND customer_id = %%d)", 
                [self.purchaseID, self.customer])
Centurion answered 26/2, 2013 at 13:51 Comment(1)
I'm having the same problem, but documentation for raw queries says to use one % for substitution.Solenne
N
2

Depending on your SQL package, you may need to use cursor.statement instead.

Nan answered 25/5, 2020 at 4:8 Comment(0)
T
1

Worked for me using double %%

  "SELECT  title, address from table t1, table t2 on t1.id=t2.id where t1.title like '%%Brink%%' "
Titled answered 24/10, 2015 at 17:35 Comment(1)
For the person who downvoted this, care to explain why?Titled
S
0
from django.db import connection
print(connection.queries)

The code above should display all the requeries that are executed on the request.

Sabaean answered 23/2, 2021 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.