Skip to content

Executing Queries

Execute SQL queries against Komodo’s Snowflake data warehouse using standard DB-API 2.0 patterns.

All Komodo data you are subscribed to lives in a Komodo-managed Snowflake warehouse provisioned for your account (accounts). The SDK gives you a DB-API 2.0 connection to that warehouse, so you can use familiar patterns: cursor.execute(), fetchone(), fetchall(), fetchmany(), and cursor.description for column metadata. That same connection works with pandas (read_sql, fetch_pandas_all, fetch_pandas_batches) and other tools that expect a standard Python database connection.

For more context on authentication and account selection, see Authentication.

The get_snowflake_connection() function returns a DB-API 2.0 compliant connection:

from komodo import get_snowflake_connection
conn = get_snowflake_connection()

Use cursors to execute queries:

cursor = conn.cursor()
# Execute a query
cursor.execute("USE DATABASE DATA")
cursor.execute("SELECT column_name, table_name FROM INFORMATION_SCHEMA.COLUMNS LIMIT 20")
# Fetch results
rows = cursor.fetchall()
print(f"Found {len(rows)} columns")
cursor.close()
conn.close()
cursor.execute("SELECT * FROM INFORMATION_SCHEMA.COLUMNS")
all_rows = cursor.fetchall()
cursor.execute("SELECT * FROM INFORMATION_SCHEMA.COLUMNS LIMIT 1")
row = cursor.fetchone()
print(row)
cursor.execute("SELECT * FROM INFORMATION_SCHEMA.COLUMNS LIMIT 100")
# Fetch 10 rows at a time
batch = cursor.fetchmany(size=10)

The Komodo CLI provides commands for executing SQL statements without writing Python code. These are useful for quick ad-hoc queries and exploration.

Before using the SQL CLI commands, authenticate and set your account:

Terminal window
uv run komodo login
Terminal window
uv run komodo account set

Run a SQL statement directly from the command line with sql-execute:

Terminal window
uv run komodo sql-execute "SELECT column_name, table_name FROM INFORMATION_SCHEMA.COLUMNS LIMIT 20"

Results are displayed in a formatted table.

For running multiple queries in a session, use the interactive sql-shell:

Terminal window
uv run komodo sql-shell

The shell prompts you to enter SQL statements. Type exit when finished. Each query’s results are displayed as a table. Errors in one statement do not exit the shell—you can continue running queries. The --environment / -E flag works the same as with sql-execute.

When a query fails, the SDK now surfaces the full Snowflake error — message, error code, SQL state, and query ID — instead of a generic message. A failed cursor.execute() raises with detail like:

Komodo Error: Failure during expansion of view 'MEDICAL_SERVICE_LINES_LATEST': Error in secure object. | Error code: 002003 | SQL state: 02000 | Query ID: 01b... (Trace ID: ...)

To investigate a query after the fact, use its query ID (cursor.sfqid, also shown as Query ID: in the error) to fetch diagnostics:

from komodo import get_snowflake_connection, get_query_diagnostics
conn = get_snowflake_connection()
cursor = conn.cursor()
cursor.execute("USE DATABASE DATA")
try:
cursor.execute("SELECT * FROM SOME_SECURE_VIEW")
except Exception as err:
print(err) # full Snowflake error, with Query ID
diagnostics = get_query_diagnostics(conn, cursor.sfqid)
print(diagnostics) # error code/message, execution status, warehouse, timing

get_query_diagnostics reads Snowflake’s INFORMATION_SCHEMA.QUERY_HISTORY for the current role/account, so a database must be set on the connection (e.g. USE DATABASE DATA) and the query must be within the history retention window. It returns None if no matching query is found. See Troubleshooting for more.

Terminal window
uv run komodo sql-diagnostics <query-id>

If you use the MCP server, an AI assistant can call the list_snowflake tool to browse databases, schemas, tables, and columns for you. That helps you discover object names before you write SQL in Python or the CLI.