SQLite Database

Submodules

Module contents

Database

class sqlite_database.Column(name: str, type_: str, foreign: bool = False, foreign_ref: str | None = None, primary: bool = False, unique: bool = False, nullable: bool = True, default: Any = None, on_delete: Literal['null', 'cascade', 'no act', 'default', 'restrict'] = 'cascade', on_update: Literal['null', 'cascade', 'no act', 'default', 'restrict'] = 'cascade')[source]

Bases: object

tip: for foreign_ref, you can split with / to separate table and column name. e.g: user/id

property default

Default value

property foreign

Is foreign enabled?

property name

Column Name

property nullable

Nullable

property on_delete

Delete setting

property on_update

Update setting

property primary

Is primary or not?

property raw_source

Source / Foreign Reference

property source

Source / Foreign Reference

property source_column

Source column / Foreign reference column

property type

Type

property unique

Is unique

class sqlite_database.Database(path: str, **kwargs)[source]

Bases: object

Sqlite3 database, this provide basic integration.

check_table(table: str)[source]

Check if table is exists or not.

close()[source]

Close database

property closed

Is database closed?

create_table(table: str, columns: Iterable[Column] | Iterable[BuilderColumn])[source]

Create table

Parameters:
  • table (str) – Table name

  • columns (Iterable[Column]) – Columns for table

Returns:

Newly created table

Return type:

Table

cursor() WithCursor[source]

Create cursor

delete_table(table: str)[source]

Delete an existing table

Parameters:

table (str) – table name

property path

Path to SQL Connection

rename_table(old_table: str, new_table: str) Table[source]

Rename existing table to a new one.

reset_table(table: str, columns: Iterable[Column] | Iterable[BuilderColumn]) Table[source]

Reset existing table with new, this rewrote entire table than altering it.

property sql

SQL Connection

table(table: str, _Database__columns: Iterable[Column] | None = None)[source]

fetch table

tables() tuple[Table, ...][source]

Return tuple containing all table except internal tables

class sqlite_database.Row[source]

Bases: dict

Attribute Dictionary

class sqlite_database.Table(parent, table: str, _Table__columns: Iterable[Column] | None = None)[source]

Bases: object

Table. Make sure you remember how the table goes.

add_column(column: Column | BuilderColumn)[source]

Add column to table

columns()[source]

Table columns

delete(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None, limit: int = 0, order: dict[str, Literal['asc', 'desc']] | None = None)[source]

Delete row or rows

Parameters:
  • condition (Condition, optional) – Condition to determine deletion See Signature class about conditional stuff. Defaults to None.

  • limit (int, optional) – Limit deletion by integer. Defaults to 0.

  • order (Optional[Orders], optional) – Order of deletion. Defaults to None.

Returns:

Rows affected

Return type:

int

delete_one(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None, order: dict[str, Literal['asc', 'desc']] | None = None)[source]

Delete a row

Parameters:
  • condition (Condition, optional) – Conditional to determine deletion.

  • None. (Defaults to)

  • order (Optional[Orders], optional) – Order of deletion. Defaults to None.

property deleted

Is table deleted

exists(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None)[source]

Check if data is exists or not.

Parameters:

condition (Condition, optional) – Condition to use. Defaults to None.

force_nodelete()[source]

Force “undelete” table. Used if table was mistakenly assigned as deleted.

get_namespace() Type[TypicalNamedTuple][source]

Generate or return pre-existed namespace/table.

insert(data: dict[str, Any])[source]

Insert data to current table

Parameters:

data (Data) – Data to insert. Make sure it’s compatible with the table.

Returns:

Last rowid

Return type:

int

insert_many(datas: list[dict[str, Any]])[source]

Alias to insert_multiple

insert_multiple(datas: list[dict[str, Any]])[source]

Insert multiple values

Parameters:

datas (Iterable[Data]) – Data to be inserted.

property name

Table name

paginate_select(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None, only: tuple[str, ...] | Literal['*'] = '*', page: int = 0, length: int = 10, order: dict[str, Literal['asc', 'desc']] | None = None, squash: Literal[False] = False) Generator[list[Row[str, Any]], None, None][source]
paginate_select(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None, only: tuple[str, ...] | Literal['*'] = '*', page: int = 0, length: int = 10, order: dict[str, Literal['asc', 'desc']] | None = None, squash: Literal[True] = True) Generator[Row[str, list[Any]], None, None]

Paginate select

Parameters:
  • condition (Condition, optional) – Confitions to use. Defaults to None.

  • only (OnlyColumn, optional) – Select what you want. Default to None.

  • page (int) – Which page number be returned first

  • length (int, optional) – Pagination length. Defaults to 10.

  • order (Optional[Orders], optional) – Order. Defaults to None.

Yields:

Generator[Queries, None, None] – Step-by-step paginated result.

rename_column(old_column: str, new_column: str)[source]

Rename existing column to new column

select(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None, only: tuple[str, ...] | Literal['*'] = '*', limit: int = 0, offset: int = 0, order: dict[str, Literal['asc', 'desc']] | None = None, squash: Literal[False] = False) list[Row[str, Any]][source]
select(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None, only: tuple[str, ...] | Literal['*'] = '*', limit: int = 0, offset: int = 0, order: dict[str, Literal['asc', 'desc']] | None = None, squash: Literal[True] = True) Row[str, list[Any]]
select(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None, only: ParsedFn = _null, limit: int = 0, offset: int = 0, order: dict[str, Literal['asc', 'desc']] | None = None, squash: Literal[False] = False) Any

Select data in current table. Bare .select() returns all data.

Parameters:
  • condition (Condition, optional) – Conditions to used. Defaults to None.

  • only – (OnlyColumn, ParsedFn, optional): Select what you want. Default to None.

  • limit (int, optional) – Limit of select. Defaults to 0.

  • offset (int, optional) – Offset. Defaults to 0

  • order (Optional[Orders], optional) – Selection order. Defaults to None.

  • squash (bool) – Is it squashed?

Returns:

Selected data

Return type:

Queries

select_one(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None, only: ParsedFn = _null, order: dict[str, Literal['asc', 'desc']] | None = None) Any[source]
select_one(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None, only: tuple[str, ...] | Literal['*'] = '*', order: dict[str, Literal['asc', 'desc']] | None = None) Row[str, Any]

Select one data

Parameters:
  • condition (Condition, optional) – Condition to use. Defaults to None.

  • only – (OnlyColumn, optional): Select what you want. Default to None.

  • order (Optional[Orders], optional) – Order of selection. Defaults to None.

Returns:

Selected data

Return type:

Any

update(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None, data: dict[str, Any] | None = None, limit: int = 0, order: dict[str, Literal['asc', 'desc']] | None = None)[source]

Update rows of current table

Parameters:
  • data (Data) – New data to update

  • condition (Condition, optional) – Condition dictionary. See Signature about how condition works. Defaults to None.

  • limit (int, optional) – Limit updates. Defaults to 0.

  • order (Optional[Orders], optional) – Order of change. Defaults to None.

Returns:

Rows affected

Return type:

int

update_one(condition: dict[str, Signature | ParsedFn | Any] | list[tuple[str, Signature | ParsedFn]] | None = None, new_data: dict[str, Any] | None = None, order: dict[str, Literal['asc', 'desc']] | None = None) int[source]

Update 1 data only

sqlite_database.blob(name: str) BuilderColumn[source]

Create a blob column with name

sqlite_database.integer(name: str) BuilderColumn[source]

Create a integer column with name

sqlite_database.real(name: str) BuilderColumn[source]

Create a real column with name

sqlite_database.text(name: str) BuilderColumn[source]

Create a text column with name