To delete data in Snowflake using Python, you can use the snowflake-connector-python
package. Here’s a sample code to delete 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() # Delete query delete_query = """ DELETE FROM your_table WHERE condition_column = 'condition_value' """ # Execute the delete query cursor.execute(delete_query) # Commit the changes conn.commit() # Close the cursor and the connection cursor.close() conn.close() print("Data deleted successfully!")
In the code above, replace 'your_username'
, 'your_password'
, 'your_account_url'
, 'your_warehouse'
, 'your_database'
, 'your_schema'
, 'your_table'
, 'condition_column'
, and 'condition_value'
with the actual values corresponding to your Snowflake account, warehouse, database, schema, table, column, 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.
A delete query is defined with the condition to identify the rows to be deleted.
The cursor.execute()
method is used to execute the delete query.
After deleting 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.