Read a result with the Python connector
The Python connector executes SQL and returns results through a cursor.
Understand the idea
Use a supported Python environment and install snowflake-connector-python according to its official installation guide. Import snowflake.connector in that same environment. An import error usually means the package is unavailable to the interpreter executing the program.
| Concept | What it means |
|---|---|
| Install | Use snowflake-connector-python in the interpreter running the script. |
| Connect | Load an approved named connection and authentication configuration. |
| Execute/fetch | cursor.execute submits SQL; fetchall retrieves rows. |
| Finish | Capture sfqid and close the cursor and connection. |
A concrete example
cursor.execute("SELECT %s::NUMBER", (101,)) binds 101 as a value. Fetching returns one logical numeric row.
Remember: Bind values separately from SQL structure and release resources on errors too.
Explain it step by step · full theory
A Python connector opens a database session
The Snowflake Connector for Python lets a Python program connect, execute statements and fetch results through database APIs. It is useful for scripts, data integrations and applications that need SQL access.
Use a supported Python environment and install snowflake-connector-python according to its official installation guide. Import snowflake.connector in that same environment. An import error usually means the package is unavailable to the interpreter executing the program.
Configure authentication and context explicitly
The example expects a named learning connection in the connector’s supported connections.toml configuration. Set account, user and context there, using authentication supported by your organization and installed connector. Follow the connection guide for the location and fields used by your operating system.
A named connection allows the lesson to show the query lifecycle without embedding credentials in the source file. Human SSO and unattended service authentication have different prerequisites. Choose a documented method for the actual caller.
Trace execute, fetch and close
connect creates the session. cursor creates an object that executes SQL and retrieves results. execute submits the statement, fetchall returns the rows and sfqid records the associated query ID. A finally block releases the cursor and connection even when a query raises an exception.
The example binds a numeric value through the connector’s default parameter style. The SQL structure remains fixed while the value is supplied separately. Expect one row containing 101, with representation determined by the connector’s numeric conversion.
When an error occurs, record a redacted message, the intended account/context and any available query ID. A connection error can occur before a query ID exists. A successful connection followed by an object error points you toward role, namespace and object privileges.
Words used in this lesson
- Cursor
- A client object for executing statements and reading results.
- Binding
- Supplying data values separately from SQL structure.
- Connector
- A library implementing database access for a programming environment.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Read a result with the Python connector
Connect
Use the configured connection identity.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Connect
Use the configured connection identity.
- Execute
Bind a value and fetch the response.
- Finish
Record query identity and release resources.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Optional client path. This lesson assumes basic Python knowledge and a configured Python environment. Beginners can use Snowsight first and return here later; never put real credentials into this website.
Optional package installation in your chosen environment
python -m pip install snowflake-connector-pythonWhat to expect and why
Use the same supported Python interpreter for installation and execution. Check the official compatibility and installation instructions before manual practice.
A parameterized Python query
import snowflake.connector
connection = snowflake.connector.connect(connection_name="learning")
try:
cursor = connection.cursor()
try:
cursor.execute("SELECT %s::NUMBER AS ORDER_ID", (101,))
print(cursor.fetchall())
print("Query ID:", cursor.sfqid)
finally:
cursor.close()
finally:
connection.close()What to expect and why
Requires a configured learning connection. The expected logical result is one ORDER_ID value of 101. The query ID is generated by your execution and cannot be predicted here.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Explain each step of the Python example and the expected result.
Compare your reasoning
Open the named connection, create a cursor, bind 101, execute the SELECT, fetch its single row, capture the query ID and close both resources.
Official sources and curriculum
Module 01 · See module scope