2012-10-02 7 views
10

ho questo cursoreErrore: Il cursore 'oggetto non ha attributo' _last_executed

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

ottengo questo errore

'Cursor' object has no attribute '_last_executed' 

Ma quando provo questo:

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

c'è nessun errore. Come posso risolvere questo?

+0

Hai finalmente farlo funzionare? – juankysmith

+3

Ho avuto lo stesso messaggio di errore, ma utilizzando l'ORM. Si è scoperto che il problema del charset è stato risolto con l'incapsulamento della stringa con unicode(). So che non risponde alla tua domanda, ma potrebbe adattarsi ad altri che atterrano su questa pagina alla ricerca di risposte. –

+0

Grazie per aver postato Tommy, mi ha sicuramente aiutato – Steve

risposta

5

Il problema è che non si stanno facendo correttamente le sostituzioni nella stringa di selezione. Dalla documentazione:

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 

    """ 

Quindi, dovrebbe essere:

cursor.execute("SELECT price FROM Items WHERE itemID = ( 
       SELECT item_id FROM Purchases 
       WHERE purchaseID = ? AND customer_id = ?)", 
       (self.purchaseID, self.customer)) 
+0

Si produce ancora lo stesso errore. – skinnyas123

+0

quale versione di python stai usando? – juankysmith

+0

Sto usando Python 2.7.2+ – skinnyas123

8

ho incontrato anche questo problema. Ho cambiato il% d in% s, ed è stato risolto. Vorrei che questo fosse utile per te.

+0

Questo ha funzionato per me. Suppongo che tu debba passare '% s' anche per gli ints. – user2233706

2

Il motivo è che si sta utilizzando '% d'. Quando si utilizza '%' in SQL, l'esecuzione interpreterà '%' come formato. Si dovrebbe scrivere la sua dichiarazione come questa:

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

Ho lo stesso problema, ma la documentazione per [query non elaborate] (https://docs.djangoproject.com/en/1.5/topics/db/sql/#executing-custom-sql-directly) dice di usarne una '%' per la sostituzione. – user2233706

+0

Questo ha funzionato per me in una situazione simile – Himanshu

1

ha funzionato per me utilizzo di un doppio %%

"SELECT title, address from table t1, table t2 on t1.id=t2.id where t1.title like '%%Brink%%' " 
+1

Per la persona che ha downvoted questo, è importante spiegare perché? – Ram

Problemi correlati