使用 psycopg2 批量更新 Postgres DB 中的行

pir*_*pir 5 python postgresql psycopg2

我们需要对 Postgres 数据库中的许多行进行批量更新,并希望使用下面的 SQL 语法。我们如何使用 psycopg2 做到这一点?

UPDATE table_to_be_updated
SET msg = update_payload.msg
FROM (VALUES %(update_payload)s) AS update_payload(id, msg)
WHERE table_to_be_updated.id = update_payload.id
RETURNING *
Run Code Online (Sandbox Code Playgroud)

尝试 1 - 传递值

我们需要将嵌套的可迭代格式传递给 psycopg2 查询。对于update_payload,我尝试传递列表列表、元组列表和元组元组。这一切都因各种错误而失败。

尝试 2 - 使用 __conform__ 编写自定义类

我试图编写一个我们可以用于这些操作的自定义类,它将返回

(VALUES (row1_col1, row1_col2), (row2_col1, row2_col2), (...))
Run Code Online (Sandbox Code Playgroud)

我已经按照此处的说明进行编码,但很明显我做错了什么。例如,在这种方法中,我必须处理表内所有值的引用,这会很麻烦并且容易出错。

class ValuesTable(list):
    def __init__(self, *args, **kwargs):
        super(ValuesTable, self).__init__(*args, **kwargs)

    def __repr__(self):
        data_in_sql = ""
        for row in self:
            str_values = ", ".join([str(value) for value in row])
            data_in_sql += "({})".format(str_values)
        return "(VALUES {})".format(data_in_sql)

    def __conform__(self, proto):
        return self.__repr__()

    def getquoted(self):
        return self.__repr__()

    def __str__(self):
        return self.__repr__()
Run Code Online (Sandbox Code Playgroud)

编辑:如果可以使用另一种语法以更快/更清晰的方式进行批量更新,而不是我原来的问题中的语法,那么我全神贯注!

Ste*_*cht 15

要求:

  • Postgres 表,由字段 id 和 msg(以及可能的其他字段)组成
  • 包含 msg 新值的 Python 数据
  • Postgres 表应该通过 psycopg2 更新

示例表

CREATE TABLE einstein(
   id CHAR(5) PRIMARY KEY,
   msg VARCHAR(1024) NOT NULL
);
Run Code Online (Sandbox Code Playgroud)

测试数据

INSERT INTO einstein VALUES ('a', 'empty');
INSERT INTO einstein VALUES ('b', 'empty');
INSERT INTO einstein VALUES ('c', 'empty');
Run Code Online (Sandbox Code Playgroud)

蟒蛇程序

假设的、自包含的示例程序,引用了一位著名物理学家的名言。

import sys
import psycopg2
from psycopg2.extras import execute_values


def print_table(con):
    cur = con.cursor()
    cur.execute("SELECT * FROM einstein")
    rows = cur.fetchall()
    for row in rows:
        print(f"{row[0]} {row[1]}")


def update(con, einstein_quotes):
    cur = con.cursor()
    execute_values(cur, """UPDATE einstein 
                           SET msg = update_payload.msg 
                           FROM (VALUES %s) AS update_payload (id, msg) 
                           WHERE einstein.id = update_payload.id""", einstein_quotes)
    con.commit()


def main():
    con = None
    einstein_quotes = [("a", "Few are those who see with their own eyes and feel with their own hearts."),
                       ("b", "I have no special talent. I am only passionately curious."),
                       ("c", "Life is like riding a bicycle. To keep your balance you must keep moving.")]

    try:
        con = psycopg2.connect("dbname='stephan' user='stephan' host='localhost' password=''")
        print_table(con)
        update(con, einstein_quotes)
        print("rows updated:")
        print_table(con)

    except psycopg2.DatabaseError as e:

        print(f'Error {e}')
        sys.exit(1)

    finally:

        if con:
            con.close()


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

准备好的报表替代

import sys
import psycopg2
from psycopg2.extras import execute_batch


def print_table(con):
    cur = con.cursor()
    cur.execute("SELECT * FROM einstein")
    rows = cur.fetchall()
    for row in rows:
        print(f"{row[0]} {row[1]}")


def update(con, einstein_quotes, page_size):
    cur = con.cursor()
    cur.execute("PREPARE updateStmt AS UPDATE einstein SET msg=$1 WHERE id=$2")
    execute_batch(cur, "EXECUTE updateStmt (%(msg)s, %(id)s)", einstein_quotes, page_size=page_size)
    cur.execute("DEALLOCATE updateStmt")
    con.commit()


def main():
    con = None
    einstein_quotes = ({"id": "a", "msg": "Few are those who see with their own eyes and feel with their own hearts."},
                       {"id": "b", "msg": "I have no special talent. I am only passionately curious."},
                       {"id": "c", "msg": "Life is like riding a bicycle. To keep your balance you must keep moving."})

    try:
        con = psycopg2.connect("dbname='stephan' user='stephan' host='localhost' password=''")
        print_table(con)
        update(con, einstein_quotes, 100)  #choose some meaningful page_size here
        print("rows updated:")
        print_table(con)

    except psycopg2.DatabaseError as e:

        print(f'Error {e}')
        sys.exit(1)

    finally:

        if con:
            con.close()


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

输出

上述程序将向调试控制台输出以下内容:

a     empty
b     empty
c     empty
rows updated:
a     Few are those who see with their own eyes and feel with their own hearts.
b     I have no special talent. I am only passionately curious.
c     Life is like riding a bicycle. To keep your balance you must keep moving.
Run Code Online (Sandbox Code Playgroud)

  • 也许值得指出的是,“executemany”[并不比在循环中调用“execute”更快](https://www.psycopg.org/docs/cursor.html#cursor.executemany)。它不会产生 OP 要求的 SQL 语法。仍然对青春时光和一个令人钦佩的男人的好语录表示赞赏。 (2认同)

Ali*_*jad 8

简短的回答!使用execute_values(curs, sql, args),参见文档

对于那些寻找简短直接答案的人。批量更新用户的示例代码;

from psycopg2.extras import execute_values

sql = """
    update users u
    set
        name = t.name,
        phone_number = t.phone_number
    from (values %s) as t(id, name, phone_number)
    where u.id = t.id;
"""

rows_to_update = [
    (2, "New name 1", '+923002954332'),
    (5, "New name 2", '+923002954332'),
]
curs = conn.cursor()  # Assuming you already got the connection object
execute_values(curs, sql, rows_to_update)
Run Code Online (Sandbox Code Playgroud)

如果您使用uuidfor 主键,并且尚未在 psycopg2 中注册uuid数据类型(将 uuid 保留为 python 字符串),则始终可以使用此条件u.id = t.id::uuid