【问题标题】:AttributeError: 'psycopg2.extensions.cursor' object has no attribute 'fast_executemany'AttributeError:“psycopg2.extensions.cursor”对象没有属性“fast_executemany”
【发布时间】:2019-05-24 17:15:03
【问题描述】:

AttributeError: 'psycopg2.extensions.cursor' 对象没有属性 'fast_executemany'

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

AttributeError: 'psycopg2.extensions.cursor' 对象没有属性 '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()

【问题讨论】:

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


    【解决方案1】:

    使用带有元组的插入比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');
    

    【讨论】:

    • 我有 13 列和 900K 行的 csv 格式。我正在和熊猫一起阅读。当我运行上面的代码时,我得到了提到的错误。我是 mogrify() 的新手。你能详细说明你的观点吗? github.com/pandas-dev/pandas/issues/15276
    • 分批剪切 1k 行并由它们迭代@RAVIVERMA
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    相关资源
    最近更新 更多