5

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

1 回答 1

4

executemany使用带有元组的插入,它比in 快 200 倍psycopg

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');
于 2018-12-26T12:59:44.250 回答