-
-
Save hivefans/6ae2e9e4df6180902e5222dae6678b95 to your computer and use it in GitHub Desktop.
Python MySQL upsert|-|{"files":{"requirements.txt":{"env":"plain"},"upsert.py":{"env":"plain"},"demo.py":{"env":"plain"}},"tag":"Uncategorized"}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MySQL-python>=1.2.5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment