我想使用python从mysql数据库中的近500个表中删除一些行。
我知道查询应该是这样的:
DELETE FROM(表名)
[WHERE条件] [ORDER BY ...] [LIMIT rows]
但是当我循环遍历表格时,我不确定我应该将什么用于表名!
这是我的循环
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()
我收到了一个错误:
您的SQL语法有错误;检查手册......等等
答案 0 :(得分:0)
用这个测试....你没有把from
放在你的查询语句上。
for table in table_names:
cursor.execute("DELETE FROM"+ table +"WHERE Expiration = Datadate AND UnderlyingSymbol = 'SPY'")