# -*- 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()
Using the code above, I would like to search the table rows with regular expressions, and search for the keyword "hello".
If it's matched, I'd like to delete the row which for loop in rows that fetched all.
How can I write the delete statement when the regular expression found the row?
Thanks very much!