ormlite package

Module contents

class ormlite.Row(model, extra)[source]

Bases: Generic[Model]

extra: dict
model: TypeVar(Model)
ormlite.connect_to_sqlite(file_name)[source]

Opens a new sqlite connection in Auto-commit mode.

Parameters:

file_name (str) – sqlite database file to open (or create if it doesn’t exist yet). Use the special argument of “:memory:” to only hold the database in memory and skip writing to file.

Return type:

Connection

ormlite.field(*, pk=False, fk=None, **kwargs)[source]
ormlite.migrate(db)[source]

Warning: This will destructively delete your data. Don’t use this if want to keep old tables data around even if there’s no corresponding python model.

Your python code is treated as the source of truth, and migrate forces your sqlite schema to match.

Every python class with the @model decorator is synced with a sqlite table. Every sqlite table that has no corresponding model is deleted.

Specifically, this:
  • Creates new tables to match new models

  • Drops tables that don’t correspond to any defined models

  • Adds new columns to existing tables

  • Drops old columns from existing tables

ormlite.model(sql_table_name)[source]
ormlite.select(model)[source]

Begin a select query. Literally just an alias for the SelectQuery constructor.

Parameters:

model (type[TypeVar(Model)]) – Model class; this determines which model is when binding the sql rows into python objects

Return type:

SelectQuery[TypeVar(Model)]

ormlite.upsert(db, records, *, update)[source]

Insert records, on conflict, update fields but only specific ones

Parameters:
  • db (DatabaseConnection) – A sqlite database connection

  • records (list[TypeVar(Model)]) – Records to insert or update

  • update (list[str]) – List of column names to update in case of conflict

ormlite.query module

class ormlite.query.SelectQuery(model)[source]

Select query builder. This object is mutable and chainable way to describe a sql query. These methods mutate the builder and return the builder to continue the chain:

  • extra

  • where

  • limit

  • join

  • order_by

These methods execute the current query:
  • rows

  • models

  • dicts

dicts(db)[source]
Return type:

list[dict[str, Any]]

extra(*fields)[source]
Return type:

SelectQuery[TypeVar(Model)]

join(model, /)[source]

Join with another table. :param: model

Return type:

SelectQuery[TypeVar(Model)]

limit(limit, /)[source]

Applies a sql LIMIT. Note that calling this multiple times on the same query, will override the previously set limit.

Return type:

SelectQuery[TypeVar(Model)]

models(db)[source]
Return type:

list[TypeVar(Model)]

order_by(clause, /)[source]
Return type:

SelectQuery[TypeVar(Model)]

rows(db)[source]
Return type:

list[Row[TypeVar(Model)]]

where(condition=None, /, **kwargs)[source]
Return type:

SelectQuery[TypeVar(Model)]

ormlite.errors module

exception ormlite.errors.InvalidForeignKeyError[source]

Bases: Exception

exception ormlite.errors.MissingAdapterError[source]

Bases: Exception

exception ormlite.errors.MultiplePrimaryKeysError[source]

Bases: Exception

exception ormlite.errors.RegexMatchError[source]

Bases: Exception