def insert_many(conn, tablename, column_names, records, chunksize=2500):
    """Insert many records by chunking data into insert statements.

    Notes
    -----
    records should be Iterable collection of namedtuples or tuples.
    """

    groups = chunks(records, chunksize)
    column_str = ','.join(column_names)
    insert_template = 'INSERT INTO {table} ({columns}) VALUES {values}'.format(
        table=tablename, columns=column_str, values='{0}')

    with conn:
        with conn.cursor() as cursor:
            for recs in groups:
                record_group = list(recs)
                records_template_str = ','.join(['%s'] * len(record_group))
                insert_query = insert_template.format(records_template_str)
                cursor.execute(insert_query, record_group)
def insert_many ( conn , tablename , column_names , records , chunksize = 2500 ) : groups = chunks ( records , chunksize ) column_str = ',' . join ( column_names ) insert_template = 'INSERT INTO {table} ({columns}) VALUES {values}' . format ( table = tablename , columns = column_str , values = '{0}' ) with conn : with conn . cursor ( ) as cursor : for recs in groups : record_group = list ( recs ) records_template_str = ',' . join ( [ '%s' ] * len ( record_group ) ) insert_query = insert_template . format ( records_template_str ) cursor . execute ( insert_query , record_group )
Insert many records by chunking data into insert statements.

    Notes
    -----
    records should be Iterable collection of namedtuples or tuples.