Skip to content

MySQL Adapter

Tip

turu-mysql depends on aiomysql.

Installation

pip install "turu[mysql]"

Usage

Basic Usage

import pydantic
import turu.mysql


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


connection = turu.mysql.connect_from_env()

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

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

Parameters Usage

format style

import pydantic
import turu.mysql


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


connection = turu.mysql.connect_from_env()

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

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

Keyword Parameters Usage

pyformat style

import pydantic
import turu.mysql


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


connection = turu.mysql.connect_from_env()

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

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