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

标签 python pandas amazon-redshift pandas-to-sql

属性错误:“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/

相关文章:

python - 使用适当的变量类型将 CSV 文件从 redshift 导出到本地

amazon-web-services - 如何在 AWS 上对 S3 数据运行删除和插入查询

python - 在另一个列表上应用 lambda 函数列表时的意外行为

python - 使用 python 更改 netcdf 文件中的维度名称

python - 在 Pandas 中选择不包含特定字符的行

python - 过滤列表列的列表,然后在 Python 中逐行拆分(分解)

python - 使用 Pandas 计算数据框中列的总和

python-2.7 - 无法使用 psycopg2 从 Amazon Redshift 读取数据

python - 长/宽数据到宽/长

python - 有没有办法在 sklearn 中进行 LabelBinarizer 变换后跟踪哪个 DataFrame 列对应于哪个数组列?