pymysql INSERT ON DUPLICATE KEY UPDATE

2023-12-13 12:43:02
def _replace_data(df, keys = ['columnId']):
        df = df.where(df.notnull(), None)
        conn = get_db_conn()
        cur = conn.cursor()
        sql = f"""INSERT INTO alg_cluster_info({','.join(df.columns)})
        VALUES({','.join(['%s']*len(df.columns))}) on duplicate key update """ + \
        ','.join([f"{i} = values({i})" for i in df.columns if i not in keys])
        print(sql)
        try:
            cur.executemany(sql, [tuple(x) for x in df.values])
            conn.commit()
        except TypeError:
            conn.rollback()
        finally:
            cur.close()
            conn.close()

文章来源:https://blog.csdn.net/itnerd/article/details/134967535
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。