# -*- coding: utf-8 -*-
import re
import sys
import MySQLdb
from getpass import getpass
reload(sys)
sys.setdefaultencoding('utf-8')
conn = MySQLdb.connect(host, user, passwd, db, charset = 'utf-8')
cur = conn.cursor()
cur.execute("show tables")
tablenames = [i[0] for i in cur.fetchall()]
cur.execute("SELECT * FROM %s" % tablenames)
rows = cur.fetchall()
for row in rows:
x = re.compile(r"\bhello\b")
p = x.search(str(row))
if p:
cur.execute("DELETE FROM %s WHERE " % t) # how to delete this row
conn.close()
使用上面的代码,我想用正则表达式搜索表格行,并搜索关键字“hello”。
如果匹配,我想删除在获取所有行的行中循环的行。
当正则表达式找到行时,如何编写删除语句?
非常感谢!