diff --git a/sqlExport.py b/sqlExport.py index e55a6ed..e117f04 100644 --- a/sqlExport.py +++ b/sqlExport.py @@ -22,19 +22,23 @@ def insertFromTableMap(table, data, tableMap): # TODO: this could probably be done better as a single statement? # note: this only works if in python >= 3.7 where `dict` is ordered - c.executemany('REPLACE INTO ' + table + ' (' + - ','.join(f'`{k}`' for k in tableMap.keys()) + - ') VALUES (' + - ','.join(len(tableMap) * ['%s']) + - ');', - list(formatRows(data))) + c.executemany( + 'INSERT INTO ' + table + ' (' + + ','.join(f'`{k}`' for k in tableMap.keys()) + + ') VALUES (' + ','.join(len(tableMap) * ['%s']) + ') ' + + 'ON DUPLICATE KEY UPDATE ' + + ', '.join(f'`{k}`=VALUES(`{k}`)' for k in tableMap.keys()) + ';', + list(formatRows(data))) # TODO: delete non-valid labels def insertLabels(members): for member in members: for label, label_id in membershipworks._parse_flags()['labels'].items(): if member[label]: - c.execute('REPLACE INTO member_labels (uid, label_id) VALUES (%s, %s);', + c.execute(""" + INSERT INTO member_labels (uid, label_id) VALUES (%s, %s) + ON DUPLICATE KEY UPDATE + uid=VALUES(uid), label_id=VALUES(label_id);""", (member['Account ID'], label_id)) else: c.execute('DELETE FROM member_labels WHERE uid=%s && label_id=%s;', @@ -91,7 +95,9 @@ try: if field.get('typ') == 8 and field['lbl'] in m: # check box m[field['lbl']] = True if m[field['lbl']] == 'Y' else False - c.executemany("REPLACE INTO labels (label, label_id) VALUES (%s, %s)", + c.executemany("""INSERT INTO labels (label, label_id) VALUES (%s, %s) + ON DUPLICATE KEY UPDATE + label=VALUES(label), label_id=VALUES(label_id);""", membershipworks._parse_flags()['labels'].items()) insertFromTableMap('members', members, tableMapping['members'])