简体   繁体   中英

why delete in SQL query in PYTHON doesn't commit changes in database?

DELETE FROM ... doesn't work. The right parameters are passed to the function. No errors are returned.

I've tried to modify routing, passing parameters by POST and GET, and I've cried a lot in a fetal position.

conn = mysql.connect()
cursor = mysql.connect().cursor()
cursor.execute("SELECT * FROM food_on_the_table WHERE table_id = %s", table_id) 
food_on_the_table = cursor.fetchall()
records = cursor.fetchall() 
cursor.execute("DELETE FROM food_on_the_table WHERE row_id = %s", row_id)   
conn.commit()
result = cursor.rowcount
message = "rows affected " + str(result)    
cursor.close()

No row is deleted from the database. row_i is right, rows affected = 1 as expected.

Try this,

try:
    conn = mysql.connect()
    with conn.cursor() as cursor:
        cursor.execute("SELECT * FROM food_on_the_table WHERE table_id = %s", table_id)
        food_on_the_table = cursor.fetchall()
        records = food_on_the_table 
    with conn.cursor() as cursor:
        cursor.execute("DELETE FROM food_on_the_table WHERE row_id = %s", row_id)

    conn.commit()

finally:
    conn.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM