gpt4 book ai didi

python - 属性错误: 'psycopg2.extensions.cursor' object has no attribute 'fast_executemany'

转载 作者:行者123 更新时间:2023-12-01 08:30:21 35 4
gpt4 key购买 nike

属性错误:“psycopg2.extensions.cursor”对象没有属性“fast_executemany”

to_sql() 太慢。所以试图解决这个问题。但是当我运行以下代码时,我得到:-

AttributeError: 'psycopg2.extensions.cursor' object has no attribute 'fast_executemany'

@event.listens_for(conn, 'before_cursor_execute')
def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany):
if executemany:
cursor.fast_executemany = True
cursor.commit()

最佳答案

在元组中使用 insert 比 psycopg 中的 executemany 快约 200 倍

args_str = ','.join(cur.mogrify("(%s,%s,%s,%s,%s,%s,%s,%s,%s)", x) for x in tup)
cur.execute("INSERT INTO table VALUES " + args_str)

相当于

INSERT INTO table VALUES ('a', 'b', 'c'), ('a', 'b', 'c'), ('a', 'b', 'c'), ('a', 'b', 'c');

关于python - 属性错误: 'psycopg2.extensions.cursor' object has no attribute 'fast_executemany' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53932097/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com