Execute raw SQL in Python¶
Every Python database driver runs raw SQL the same way: a query string, its
parameters, and rows back. The driver can't tell your type checker what those
rows hold, so they arrive as Any or tuple[Any, ...], and a typo in a column
name shows up only when the query runs.
sqlty reads the query and your schema, and gives each query a row type. This page shows how to perform raw SQL queries with each common driver so that the rows are typed and the SQL checked against the schema, without a database.
All examples use this schema:
schema.sql:
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT
);
[tool.sqlty]
schema = "schema.sql"
The three steps¶
- Write the query as a string that starts with
--sqlon its own line. sqlty rewrite .wraps it inSQL[T](...),Tbeing the row type inferred from the query and the schema.SQL[T]is astr, so the driver takes it as is.sqlty check .reports queries that don't match the schema, rows annotated with the wrong type, and calls that pass the wrong parameters.
query = "--sql\nSELECT id, name, email FROM users WHERE id = %s"
# rewritten: query = SQL[tuple[int, str, str | None], tuple[int | None]]("--sql\nSELECT id, name, email FROM users WHERE id = %s")
Open in playgroundSee Writing queries for the marker and Query parameters for the parameter types.
Pass values as parameters¶
Never build a query with an f-string, % or +. A value merged into the SQL
text can change what the query does (SQL injection), and sqlty skips f-strings
because their SQL isn't known until run time. Every driver takes the values
separately, in its own placeholder style:
| Driver | Placeholders | Parameters | Calls checked |
|---|---|---|---|
sqlite3, aiosqlite |
?, :name |
a tuple, or a dict for :name |
yes |
| psycopg 3 | %s, %(name)s |
a tuple, or a dict for %(name)s |
yes |
| psycopg2 | %s, %(name)s |
a tuple, or a dict for %(name)s |
no |
| asyncpg | $1, $2 |
positional arguments after the query | yes |
| rustmy | %s, %(name)s |
a tuple, a dict for %(name)s, or a single value |
yes |
SQLAlchemy text() |
:name |
a dict | no |
Django connection.cursor() |
%s |
a list or tuple | no |
A single value still goes in a sequence, (user_id,) and not (user_id), except
with rustmy, which binds it to the query's one placeholder.
sqlty check compares the parameters a call passes with the query where the
driver is checked: see Driver calls. Elsewhere it
checks the SQL and the row type.
sqlite3¶
Connection.execute
creates a cursor and runs the query on it. sqlty checks the parameters of the call:
[tool.sqlty]
dialect = "sqlite"
import sqlite3
conn = sqlite3.connect("app.db")
user = conn.execute("SELECT id, name FROM users WHERE id = ?", (1,)).fetchone()
# error: [param-count-mismatch] "query has 2 parameters, the call passes 1"
conn.execute("UPDATE users SET name = ? WHERE id = ?", ("Ada",))
Open in playgroundpsycopg 3¶
conn.execute(query, params) is the psycopg 3 shortcut for a cursor's
execute. With sqlty.drivers.psycopg, the rows come back as the
query's T, built by the matching row factory:
from typing import NamedTuple
import psycopg
from sqlty import SQL
from sqlty.drivers.psycopg import execute
class User(NamedTuple):
id: int
name: str
# error:
by_id = SQL[User]("--sql\nSELECT id, name FROM users WHERE id = %s")
def find(conn: psycopg.Connection) -> User | None:
return execute(conn, by_id, (1,)).fetchone()
Open in playgroundA query that doesn't match its row type is reported where it is written:
from typing import NamedTuple
from sqlty import SQL
class User(NamedTuple):
id: int
name: str
# error: [column-count-mismatch] "User has 2 fields but query returns 3 columns"
by_id = SQL[User]("--sql\nSELECT id, name, email FROM users WHERE id = %s")
Open in playgroundasyncpg¶
asyncpg numbers its placeholders $1, $2 and takes the values as positional
arguments of fetch, fetchrow, fetchval and execute:
import asyncpg
async def rename(user_id: int, name: str) -> None:
conn = await asyncpg.connect()
row = await conn.fetchrow("SELECT id, name FROM users WHERE id = $1", user_id)
# error: [param-count-mismatch] "query has 2 parameters, the call passes 1"
await conn.execute("UPDATE users SET name = $1 WHERE id = $2", name)
Open in playgroundasyncpg takes only an exact str as a query, so an SQL[T] raises TypeError there.
The sqlty.drivers.asyncpg helpers pass it as a str and build each row as T:
from typing import NamedTuple
import asyncpg
from sqlty import SQL
from sqlty.drivers.asyncpg import fetchrow
class User(NamedTuple):
id: int
name: str
# error:
by_id = SQL[User]("--sql\nSELECT id, name FROM users WHERE id = $1")
async def find(conn: asyncpg.Connection, user_id: int) -> User | None:
return await fetchrow(conn, by_id, user_id)
Open in playgroundrewrite can write the class for you: see
Generated row classes.
aiosqlite¶
aiosqlite wraps sqlite3 for asyncio. A connection passed to a function is
checked when its parameter is annotated:
[tool.sqlty]
dialect = "sqlite"
import aiosqlite
async def rename(db: aiosqlite.Connection, name: str) -> None:
await db.execute("UPDATE users SET name = ? WHERE id = ?", (name, 1))
# error: [param-count-mismatch] "query has 2 parameters, the call passes 1"
await db.execute("UPDATE users SET name = ? WHERE id = ?", (name,))
Open in playgroundrustmy¶
rustmy is a MySQL and MariaDB driver with
PyMySQL's placeholders, for threads and, as rustmy.aio, for asyncio. The
cursors of its connections and pools are checked, prepared ones too:
[tool.sqlty]
dialect = "mysql"
from rustmy import aio
async def rename(pool: aio.Pool, user_id: int, name: str) -> None:
async with pool.acquire() as conn, conn.cursor() as cur:
await cur.execute("SELECT id, name FROM users WHERE id = %s", user_id)
# error: [param-count-mismatch] "query has 2 parameters, the call passes 1"
await cur.execute("UPDATE users SET name = %s WHERE id = %s", (name,))
Open in playgroundSQLAlchemy text()¶
text()
runs a raw SQL string through SQLAlchemy, with :name placeholders and a dict
of values. sqlty reads :name with the postgres and sqlite dialects:
from typing import NamedTuple
from sqlalchemy import Connection, text
from sqlty import SQL
class User(NamedTuple):
id: int
name: str
by_name = SQL[User]("--sql\nSELECT id, name FROM users WHERE name = :name")
# error: [unknown-column]
by_email = SQL[User]("--sql\nSELECT id, name FROM users WHERE mail = :email")
def find(conn: Connection, name: str) -> list[User]:
return [User(*row) for row in conn.execute(text(by_name), {"name": name})]
Open in playgroundORMs may convert some values before your code sees them, such as SQLite dates to
datetime.date. Drivers and ORMs lists those and what to
annotate.
Django¶
Django runs raw SQL through connection.cursor(), with %s placeholders for
every database backend. Mark the query and set dialect to your database (on
MySQL, write the marker as -- sql, see Writing queries):
from django.db import connection
from sqlty import SQL
def names() -> list[tuple[str]]:
with connection.cursor() as cursor:
cursor.execute(SQL[tuple[str]]("--sql\nSELECT name FROM users"))
# error: [unknown-table] "table `accounts` is not defined in the schema"
cursor.execute("--sql\nSELECT name FROM accounts")
return cursor.fetchall()
Open in playgroundCheck every query in CI¶
sqlty check . exits 1 when a rule of severity error fires, so it fits a CI job or a pre-commit
hook: see CI and pre-commit. Your type checker (mypy, pyright
or ty) then sees the row type of each SQL[T] query.