我正在尝试迭代表,获取字段具有模式的行,然后使用匹配组更新同一行。
以下代码运行没有错误,子句print
之前的两行输出正确的值。update
我遵循了类似的答案来提出该update
条款,逻辑对我来说似乎是正确的。但是代码不起作用,即没有更新行。我哪里做错了?谢谢,
# -*- coding: utf-8 -*-
import re
import MySQLdb
pattern = re.compile('@(.*)@.*$')
conn = MySQLdb.connect(
host='localhost', user='root',
passwd='password', db='j314', charset='utf8')
cursor = conn.cursor()
cursor.execute(
"""select `id`, `created_by_alias` from w0z9v_content where `catid` = 13 AND `created_by_alias` regexp "^@.*@.*$" limit 400""")
aliases = cursor.fetchall()
for alias in aliases:
newalias = pattern.match(alias[1])
if newalias.group(1) is not None:
# print alias[0]
# print newalias.group(1)
cursor.execute("""
update w0z9v_content set created_by_alias = %s where id = %s""", (newalias.group(1), alias[0]))
conn.close