0

I'm having some troubles with this python method.

I don't have any problem getting the select results but when I've tried to execute the update I don't get any results.

I have tried to generate another cursor object, redefine the cursor, generate another connection, use a different sql query (without the use of the %s) and I didn't have any results.

If you could give me any help i would be really appreciate.

def getTarea():

        conn = db.connect('url','user','pass','dbInstance')
        with conn:
                try:
                        cursor = conn.cursor(db.cursors.DictCursor)
                        sql = "SELECT CMD, ID_TAREA FROM TAREAS WHERE OBTENIDA = '0' AND DEVICE_ID = '1001' ORDER BY FECHA_TAREA DESC LIMIT 1"
                        cursor.execute(sql)

                        f.write(sql+"\n")

                        # fetch all of the rows from the query
                        data = cursor.fetchone()
                        # print the rows
                        f.write("CMD: "+data["CMD"]+"\n")
                        f.write("ID_TAREA: "+ str(data["ID_TAREA"])+"\n")
                        idTarea = str(data["ID_TAREA"])
                        obtenido = 1

                        cursor.execute("""UPDATE TAREAS SET OBTENIDA=%s WHERE ID_TAREA =%s""", (obtenido, idTarea))

                        cursor.close()
                        conn.close()
                except Exception as e:
                        f.write("error \n"+e)
        return cmd

1 Answers1

1

conn.commit() will commit the changes, as documented in this similar post: Database does not update automatically with MySQL and Python

Community
  • 1
  • 1
M. K. Hunter
  • 1,778
  • 3
  • 21
  • 27