Search code examples
pythonsqlalchemysnowflake-cloud-data-platform

Rows not getting added to Snowflake table using Python connector


I am trying to create and load a table in Snowflake using Python. The data is in a pandas data frame. Below is the code I'm using. The table gets created, but it has 0 rows and there's no error message shown.

with engine.connect() as con:
            data.head(0).to_sql(name=table_name, con=con, if_exists="replace", index=False)
            query="put file://" + file_path + "* @%" + table_name
            con.execute(text(query))
            query2="copy into " + table_name + " ON_ERROR=CONTINUE"
            con.execute(text(query2))

Some notes:

  • I had initially used the f string approach, but for some reason it was showing a "not executable object" error, so I changed that up to use text()
  • When I run the exact same code on a different computer, it works as expected and loads rows in the table, so I don't quite understand what could be happening here

Solution

  • Moving the answer from comment to answer for closure:

    You need to commit: conn.commit() or use with engine.begin() as conn: to commit automatically.

    (@snakecharmerb I can delete this answer if you want to provide one owned by you)