To update data in Snowflake using Python, you can use the snowflake-connector-python
package. Here’s a sample code to update data in Snowflake:
import snowflake.connector # Connect to Snowflake conn = snowflake.connector.connect( user='your_username', password='your_password', account='your_account_url', warehouse='your_warehouse', database='your_database', schema='your_schema' ) # Create a cursor cursor = conn.cursor() # Update query update_query = """ UPDATE your_table SET column1 = 'new_value' WHERE condition_column = 'condition_value' """ # Execute the update query cursor.execute(update_query) # Commit the changes conn.commit() # Close the cursor and the connection cursor.close() conn.close() print("Data updated successfully!")
In the code above, replace 'your_username'
, 'your_password'
, 'your_account_url'
, 'your_warehouse'
, 'your_database'
, 'your_schema'
, 'your_table'
, 'column1'
, 'new_value'
, 'condition_column'
, and 'condition_value'
with the actual values corresponding to your Snowflake account, warehouse, database, schema, table, columns, and condition.
The code establishes a connection to Snowflake using the snowflake-connector-python
package. You need to provide your Snowflake account URL, username, password, warehouse, database, and schema.
An update query is defined with the desired column and value to be updated, as well as the condition to identify the rows to be updated.
The cursor.execute()
method is used to execute the update query.
After updating the data, the changes are committed using conn.commit()
.
Finally, the cursor and the connection are closed, and a success message is printed to the console.
Make sure to have the snowflake-connector-python
package installed. You can install it using pip:
pip install snowflake-connector-python
Remember to handle any exceptions that may occur during the connection, query execution, or transaction management for proper error handling.