Connecting to existing databases
Communicate with your database connection from your code blocks.
Create a database connection to communicate with your existing data sources (e.g. MySQL, PostgreSQL, BigQuery, Snowflake, Redis) from the "Connections" tab on the "Data" page.
Once the connection is established, you can access a database in your Code Decision blocks using the context
arg:
context.run_query('db_connection_name', 'select * from my_table')
returns adict
; the query results are under the'results'
key.For safe parameterized query execution use Jinja templating (notice the
{{}}
) in the following example:context.run_query('db_connection_name', 'select * from my_table where id = {{id}}', {'id': some_id})
context.client('db_connection_name')
returns a native client for the database connection. This gives you more flexibility, but also more responsibility. Don't go dropping tables or introducing SQL injection possibilities.For a PostgreSQL connection it returns a
psycopg2
connection object.For a MySQL connection it returns a
mysql-connector-python
connection object.For a BigQuery connection it returns a
bigquery.Client
object.For a Snowflake connection it returns a
SnowflakeConnection
object.For a Redis connection it returns a
redis.Redis
object.
Last updated