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

时间:2018-12-26 12:25:18

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

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()

1 个答案:

答案 0 :(得分:1)

使用具有元组的插入比executemany中的psycopg快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');