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
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 theDROP
privilege. Logically,TRUNCATE TABLE
is similar to aDELETE
statement that deletes all rows, or a sequence ofDROP TABLE
andCREATE 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()