Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

to_sql() is too slow. so trying to resolve the problem. but when I run the following code I am getting :-

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()
like image 753
RAVI VERMA Avatar asked Dec 26 '18 12:12

RAVI VERMA


1 Answers

use insert with tuples it around 200 time faster than executemany in 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) 

its equivalent of

INSERT INTO table VALUES ('a', 'b', 'c'), ('a', 'b', 'c'), ('a', 'b', 'c'), ('a', 'b', 'c');
like image 70
frankegoesdown Avatar answered Oct 05 '22 14:10

frankegoesdown