Error: Cursor' object has no attribute '_last_executed

20,102

Solution 1

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

Solution 2

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

Solution 3

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])

Solution 4

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

Solution 5

Worked for me using double %%

  "SELECT  title, address from table t1, table t2 on t1.id=t2.id where t1.title like '%%Brink%%' "
Share:
20,102
skinnyas123
Author by

skinnyas123

Updated on December 29, 2021

Comments

  • skinnyas123
    skinnyas123 over 2 years

    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?

  • juankysmith
    juankysmith over 11 years
    what version of python are you using?
  • user2233706
    user2233706 almost 11 years
    I'm having the same problem, but documentation for raw queries says to use one % for substitution.
  • user2233706
    user2233706 almost 11 years
    This worked for me. I guess you have to pass %s even for ints.
  • Ram
    Ram about 8 years
    For the person who downvoted this, care to explain why?