Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Cursor' object has no attribute '_last_executed

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?

like image 271
skinnyas123 Avatar asked Oct 02 '12 14:10

skinnyas123


5 Answers

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

like image 186
bigwind Avatar answered Oct 06 '22 12:10

bigwind


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))
like image 20
juankysmith Avatar answered Oct 06 '22 12:10

juankysmith


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])
like image 21
muyline Avatar answered Oct 06 '22 11:10

muyline


Worked for me using double %%

  "SELECT  title, address from table t1, table t2 on t1.id=t2.id where t1.title like '%%Brink%%' "
like image 39
Ram Avatar answered Oct 06 '22 13:10

Ram


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

like image 1
Pikamander2 Avatar answered Oct 06 '22 12:10

Pikamander2