Python mysql: how do loop through table and regex-replacing field?
Question: I am trying to iterate a table, fetch the rows in which a field has a pattern,
then update the same row with a match group.

The following code runs without error, the two `print` lines before `update`
clause output correct values. I have followed similar answers to come up the
`update` clause, the logic seems right to me. However the code does not work,
i.e., no rows updated. Where did I do wrong? Thanks,

    
    
    # -*- 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
    


Answer: [`autocommit`](http://dev.mysql.com/doc/refman/5.6/en/server-system-
variables.html#sysvar_autocommit) is probably globally disabled on the server.

Execute either `COMMIT` after your updates, or `SET autocommit=1` at the
beginning of the session.

<http://dev.mysql.com/doc/refman/5.0/en/commit.html>

Also, you're not actually closing the connection, you forgot to _call_ close:

`conn.close()`

