Search code examples
python-3.xmysql-connectormysql-connector-python

MySQL Connector : could not process parameters can't delete rows


I can't use execute method

conn = connect(**cinfo)
cursor = conn.cursor()
# Drop the tables.
drop_query = 'DELETE FROM %s;'
tables = ['History', 'Product', 'Category']
for table in tables:
    cursor.execute(drop_query, (table,))
conn.commit()

I have this error:

Failed executing the operation; Could not process parameters

EDIT: I want to delete all the rows of these tables


Solution

  • Try it with TRUNCATE. It will remove all the rows in your given table. This is what MySQL Documentation mention about it.

    TRUNCATE TABLE empties a table completely. It requires the DROP privilege. Logically, TRUNCATE TABLE is similar to a DELETE statement that deletes all rows, or a sequence of DROP TABLE and CREATE TABLE statements.

    conn = connect(**cinfo)
    cursor = conn.cursor()
    # Drop the tables.
    drop_query = "TRUNCATE TABLE {};"
    tables = ['History', 'Product', 'Category']
    for table in tables:
        cursor.execute(drop_query.format(table))
    conn.commit()
    conn.close()
    

    For more information you can read this How to Delete All Rows of a MySQL Table in Python article too. Use string concatenate with format.

    conn = connect(**cinfo)
    cursor = conn.cursor()
    # Drop the tables.
    drop_query = "DELETE FROM {};"
    tables = ['History', 'Product', 'Category']
    for table in tables:
        cursor.execute(drop_query.format(table))
    conn.commit()
    conn.close()