Search code examples
pythonsqlsnowflake-cloud-data-platformsnowflake-connector

insert rows via a python generator using snowflake executemany function


I have a snowflake table and I am using the Snowflake Python Connector to insert value into it. Since I have a lot of data I am using a python generator object to insert value into my table.

conn = snowflake.connector.connect(...)
with conn.cursor() as write_cur:
    write_cur.executemany("INSERT INTO TABLE_1 VALUES (%s, %s)",
                          generator_data)

but this seems to throw an error that generator has no attribute len() . Is there anyway I can use an iterator to insert values into a snowflake table.


Solution

  • If possible don't do this - individual inserts are slow.

    Instead batch collect all the values to insert, and then in one INSERT bring in all the rows.

    But let's talk about the code in the question: There's nothing to iterate through. For cursor() to iterate, first you need results out of somewhere to iterate through.