Skip to content

SQLite3 Adapter

Tip

turu-sqlite3 depends on aiosqlite.

Installation

pip install "turu[sqlite3]"

Usage

Basic Usage

import pydantic
import turu.sqlite3


class User(pydantic.BaseModel):
    id: int
    name: str


connection = turu.sqlite3.connect(":memory:")

with connection.cursor() as cursor:
    user = cursor.execute_map(User, "SELECT 1, 'taro'").fetchone()

    assert user == User(id=1, name="taro")

Parameters Usage

qmark style

import pydantic
import turu.sqlite3


class User(pydantic.BaseModel):
    id: int
    name: str


connection = turu.sqlite3.connect(":memory:")

with connection.cursor() as cursor:
    user = cursor.execute_map(
        User,
        "SELECT ?, ?",
        [1, "taro"],
    ).fetchone()

    assert user == User(id=1, name="taro")

Keyword Parameters Usage

named style

import pydantic
import turu.sqlite3


class User(pydantic.BaseModel):
    id: int
    name: str


connection = turu.sqlite3.connect(":memory:")

with connection.cursor() as cursor:
    user = cursor.execute_map(
        User,
        "SELECT :id, :name",
        {"id": 1, "name": "taro"},
    ).fetchone()

    assert user == User(id=1, name="taro")