简体   繁体   中英

deleting data from multiple mysql tables using python

I want to delete some rows from almost 500 tables in mysql database using python.

I know that the query should be something like this: DELETE FROM (table name)
[WHERE conditions] [ORDER BY ...] [LIMIT rows]

but I am not sure what should I use for the table name when I looping over the tables!

here is my loop

from mysql.connector import (connection)
# call databasecnx = connection.MySQLConnection(user='user', password='PW',host='127.0.0.1', database= 'database')
cursor = cnx.cursor()
cursor.execute("SHOW TABLES LIKE 'options_20%'")
table_names = [tables for (tables, ) in cursor]
for t in table_names:
cursor.execute("Delete table-refs WHERE Expiration = Datadate AND UnderlyingSymbol = 'SPY'")
cnx.commit() 

I got an error:
You have an error in your SQL syntax; check the manual that...etc

Test with this.... You did'nt put from on your query statement.

for table in table_names:
cursor.execute("DELETE FROM"+ table +"WHERE Expiration = Datadate AND UnderlyingSymbol = 'SPY'")

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