Skip to content

Instantly share code, notes, and snippets.

@martinburch
Last active March 2, 2022 08:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save martinburch/c87a5eeb1ba397a0631b555dd93e7382 to your computer and use it in GitHub Desktop.
Save martinburch/c87a5eeb1ba397a0631b555dd93e7382 to your computer and use it in GitHub Desktop.
Python MySQL upsert
#!/usr/bin/env python
# encoding: utf-8
import MySQLdb
from upsert import upsert
db = MySQLdb.connect(host="localhost", user="root", passwd="", db="demo", charset="utf8")
c = db.cursor()
import warnings
warnings.filterwarnings("ignore", "Unknown table.*")
c.execute("""DROP TABLE IF EXISTS upsert_demo""")
c.execute("""CREATE TABLE upsert_demo (
`id` int(11) unsigned NOT NULL,
`foo` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
c.execute("""INSERT INTO upsert_demo
(`id`,`foo`)
VALUES (1, 'baz')""")
demo_fields = ("id","foo")
demo_objects = [(1,"upserted!"),(2,"new record")]
upsert(db,"upsert_demo",demo_fields,demo_objects)
MySQL-python>=1.2.5
#!/usr/bin/env python
# encoding: utf-8
def upsert(db, table, fields, object_list):
cursor = db.cursor()
table = "`"+db.escape_string(table)+"`"
fields = ["`"+db.escape_string(field)+"`" for field in fields]
placeholders = ["%s" for field in fields]
assignments = ["`{x}` = VALUES(`{x}`)".format(
x=db.escape_string(x)
) for x in fields]
query_string = """INSERT INTO
{table}
({fields})
VALUES
({placeholders})
ON DUPLICATE KEY UPDATE {assignments}"""
cursor.executemany(query_string.format(
table=table,
fields=", ".join(fields),
placeholders=", ".join(placeholders),
assignments=", ".join(assignments)
), object_list)
db.commit()
@bnlambert
Copy link

Thank you very much for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment