<\/span><\/h2>\n\n\n\nIn this step, your Python script connects to Snowflake and writes a single row to a table.<\/p>\n\n\n\n
This requires a simple amendment to the previous script. Replace the two sections that executes the SQL statement and loops through the result with:<\/p>\n\n\n\n
# Execute a query to write data into your table\ncur.execute(\"\"\"\n INSERT INTO <your_table_name> (column1, column2) \n VALUES ('value1', 'value2')\n\"\"\")<\/code><\/pre>\n\n\n\n<\/span>How To Secure Your Credentials With Environment Variables<\/span><\/h2>\n\n\n\nThe sample script hard-codes your credentials, which is a bad practice. It\u2019s fine for a quick test but let\u2019s get this project into shape.<\/p>\n\n\n\n
A common way to avoid hard-coding is to use environment variables. You can set these variables in your shell, and they can be accessed from Python using the os<\/em> module.<\/p>\n\n\n\nIf you run the code below on your terminal, the details will be set for a single session.<\/p>\n\n\n\n
export SNOWFLAKE_ACCOUNT=<your_account>\nexport SNOWFLAKE_USER=<your_username>\nexport SNOWFLAKE_PASSWORD=<your_password>\nexport SNOWFLAKE_WAREHOUSE=<your_warehouse>\nexport SNOWFLAKE_DATABASE=<your_database>\nexport SNOWFLAKE_SCHEMA=<your_schema><\/code><\/pre>\n\n\n\nTo make these variables persistent across sessions, you can add these lines to your shell’s profile script.<\/p>\n\n\n\n
Let\u2019s say you are using Ubuntu and you want to edit the .bashrc file. Follow these steps.<\/p>\n\n\n\n
\n- Open the file in the vi text editor: vi ~\/.bashrc<\/em><\/li>\n\n\n\n
- Press \u201cG\u201d to jump to the end of the file.<\/li>\n\n\n\n
- Press \u201ci\u201d for insert mode.<\/li>\n\n\n\n
- Start typing the lines at the end of the file.<\/li>\n\n\n\n
- Save and close by entering :wq!<\/em><\/li>\n\n\n\n
- Source the file by entering source ~\/.bashrc<\/em><\/li>\n<\/ol>\n\n\n\n
You now need to edit the connection section in your Python script like this:<\/p>\n\n\n\n
import os\n# Create a connection object\nconn = snowflake.connector.connect(\n user=os.environ.get('SNOWFLAKE_USER'),\n password=os.environ.get('SNOWFLAKE_PASSWORD'),\n account=os.environ.get('SNOWFLAKE_ACCOUNT'),\n warehouse=os.environ.get('SNOWFLAKE_WAREHOUSE'),\n database=os.environ.get('SNOWFLAKE_DATABASE'),\n schema=os.environ.get('SNOWFLAKE_SCHEMA')\n)<\/code><\/pre>\n\n\n\nDon\u2019t forget to import the os module at the top of the script.<\/p>\n\n\n\n