2013-05-02 17 views
6

Sto provando a selezionare un valore da MySQL (total_points) quindi aggiungere a una variabile locale (new_points) e aggiornare total_points nella query SAME, qualcuno sa se questo è possibile ...MySQL seleziona e aggiorna in una query

momento ho.,

cursor = database.cursor() 
cursor.execute("""SELECT total_points FROM g_ent WHERE e_id =%s AND user =%s; 
UPDATE total_points = (total_points + %s)""" 
,(e_id, user_name, new_points)) 
database.commit()  
+0

in "UPDATE total_points = (total_points +% s)' di che tabella si tratta? o se 'total_points' è la tabella, qual è il campo? – reikyoushin

risposta

1

Il problema è che la sintassi SQL non è corretta. La query dovrebbe essere:

UPDATE g_ent 
SET total_points = total_points + %s 
WHERE e_id = %s AND user = %s; 

L'esempio completo sarebbe allora:

cursor = database.cursor() 
cursor.execute("""UPDATE g_ent 
        SET total_points = total_points + %s 
        WHERE e_id = %s AND user = %s;""", 
       (new_points, e_id, user_name)) # order of params revised 
database.commit() 

prega di notare che l'ordine dei parametri di query è stato rivisto.

3
UPDATE g_ent 
SET total_points = total_points + %s 
Where e_id = %s 
    AND user = %s 
+0

plz usa la formattazione del codice – jurgenreza