python Python MySQL upsert

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 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()

以上是关于python Python MySQL upsert的主要内容,如果未能解决你的问题,请参考以下文章

python操作mysql

Python mysql

python mysql安装失败

python 操作mysql 数据库 安装 MySQL-python

python连接MySQL

[Python_6] Python 配置 MySQL 访问