テーブルを反復処理し、フィールドにパターンがある行をフェッチしてから、同じ行を一致グループで更新しようとしています。
次のコードはエラーなしで実行され、句のprint
前の 2 行で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