API

PyDbLite.PyDbLite API

class pydblite.pydblite.Index(db, field)[source]

Class used for indexing a base on a field. The instance of Index is an attribute of the Base instance

class pydblite.pydblite._Base(path, protocol=2, save_to_file=True, sqlite_compat=False)[source]

protocol as defined in pickle / pickle. Defaults to the highest protocol available. For maximum compatibility use protocol = 0

__init__(path, protocol=2, save_to_file=True, sqlite_compat=False)[source]

protocol as defined in pickle / pickle. Defaults to the highest protocol available. For maximum compatibility use protocol = 0

add_field(field, column_type='ignored', default=None)[source]

Adds a field to the database

commit()[source]

Write the database to a file

create(*fields, **kw)[source]

Create a new base with specified field names.

Args:
  • *fields (str): The field names to create.
  • mode (str): the mode used when creating the database.
  • if mode = ‘create’ : create a new base (the default value)
  • if mode = ‘open’ : open the existing base, ignore the fields
  • if mode = ‘override’ : erase the existing base and create a new one with the specified fields
Returns:
  • the database (self).
create_index(*fields)[source]

Create an index on the specified field names

An index on a field is a mapping between the values taken by the field and the sorted list of the ids of the records whose field is equal to this value

For each indexed field, an attribute of self is created, an instance of the class Index (see above). Its name it the field name, with the prefix _ to avoid name conflicts

Args:
  • fields (list): the fields to index
delete(remove)[source]

Remove a single record, or the records in an iterable

Before starting deletion, test if all records are in the base and don’t have twice the same __id__

Args:
  • remove (record or list of records): The record(s) to delete.
Returns:
  • Return the number of deleted items
delete_index(*fields)[source]

Delete the index on the specified fields

drop_field(field)[source]

Removes a field from the database

exists()[source]
Returns:
  • bool: if the database file exists
fields = None

The list of the fields (does not include the internal fields __id__ and __version__)

get_indices()[source]

Returns the indices

get_unique_ids(id_value, db_filter=None)[source]

Returns a set of unique values from column

group_by(column, torrents_filter)[source]

Returns the records grouped by column

insert(*args, **kw)[source]

Insert one or more records in the database.

Parameters can be positional or keyword arguments. If positional they must be in the same order as in the create() method If some of the fields are missing the value is set to None

Args:
  • args (values, or a list/tuple of values): The record(s) to insert.
  • kw (dict): The field/values to insert
Returns:
  • Returns the record identifier if inserting one item, else None.
name = None

The basename of the path, stripped of its extension

open()[source]

Open an existing database and load its content into memory

path = None

The path of the database in the file system

update(records, **kw)[source]

Update one record or a list of records with new keys and values and update indices

Args:
  • records (record or list of records): The record(s) to update.

PyDbLite.SQLite API

Main differences from pydblite.pydblite:

  • pass the connection to the SQLite db as argument to Table
  • in create() field definitions must specify a type.
  • no drop_field (not supported by SQLite)
  • the Table instance has a cursor attribute, so that raw SQL requests can be executed.
exception pydblite.sqlite.SQLiteError[source]
class pydblite.sqlite.Database(filename, **kw)[source]

To create an in-memory database provide ‘:memory:’ as filename

Args:
  • filename (str): The name of the database file, or ‘:memory:’
  • kw (dict): Arguments forwarded to sqlite3.connect
__enter__()[source]

Enter ‘with’ statement

__exit__(exc_type, exc_val, exc_tb)[source]

Exit ‘with’ statement

__init__(filename, **kw)[source]

To create an in-memory database provide ‘:memory:’ as filename

Args:
  • filename (str): The name of the database file, or ‘:memory:’
  • kw (dict): Arguments forwarded to sqlite3.connect
__weakref__

list of weak references to the object (if defined)

_tables()[source]

Return the list of table names in the database

close()[source]

Closes the database

commit()[source]

Save any changes to the database

conn = None

The SQLite connection

cursor = None

The SQLite connections cursor

class pydblite.sqlite.Table(table_name, db)[source]

Args:

  • table_name (str): The name of the SQLite table.
  • db (Database): The database.
__call__(*args, **kw)[source]

Selection by field values.

db(key=value) returns the list of records where r[key] = value

Args:
  • args (list): A field to filter on.
  • kw (dict): pairs of field and value to filter on.
Returns:
  • When args supplied, return a Filter object that filters on the specified field.
  • When kw supplied, return all the records where field values matches the key/values in kw.
__delitem__(record_id)[source]

Delete by record id

__getitem__(record_id)[source]

Direct access by record id.

__init__(table_name, db)[source]

Args:

  • table_name (str): The name of the SQLite table.
  • db (Database): The database.
__iter__()[source]

Iteration on the records

_get_table_info()[source]

Inspect the base to get field names.

_insert_many(args)[source]

Insert a list or tuple of records

Returns:
  • The last row id
_len(db_filter=None)[source]

Return number of matching entries

_make_record(row, fields=None)[source]

Make a record dictionary from the result of a fetch

_make_sql_params(kw)[source]

Make a list of strings to pass to an SQL statement from the dictionary kw with Python types.

add_field(name, column_type='TEXT', default=None)[source]

Add a new column to the table.

Args:
  • name (string): The name of the field
  • column_type (string): The data type of the column (Defaults to TEXT)
  • default (datatype): The default value for this field (if any)
commit()[source]

Save any changes to the database

conv(field_name, conv_func)[source]

When a record is returned by a SELECT, ask conversion of specified field value with the specified function.

create(*fields, **kw)[source]

Create a new table.

Args:
  • fields (list of tuples): The fields names/types to create. For each field, a 2-element tuple must be provided:

    • the field name
    • a string with additional information like field type + other information using the SQLite syntax eg (‘name’, ‘TEXT NOT NULL’), (‘date’, ‘BLOB DEFAULT CURRENT_DATE’)
  • mode (str): The mode used when creating the database.

    mode is only used if a database file already exists.

    • if mode = ‘open’ : open the existing base, ignore the fields
    • if mode = ‘override’ : erase the existing base and create a new one with the specified fields
Returns:
  • the database (self).
cursor = None

The SQLite connections cursor

delete(removed)[source]

Remove a single record, or the records in an iterable.

Before starting deletion, test if all records are in the base and don’t have twice the same __id__.

Returns:
  • int: the number of deleted items
get_group_count(group_by, db_filter=None)[source]

Return the grouped by count of the values of a column

get_unique_ids(unique_id, db_filter=None)[source]

Return all the unique values of a column

insert(*args, **kw)[source]

Insert a record in the database.

Parameters can be positional or keyword arguments. If positional they must be in the same order as in the create() method.

Returns:
  • The record identifier
is_date(field_name)[source]

Ask conversion of field to an instance of datetime.date

is_datetime(field_name)[source]

Ask conversion of field to an instance of datetime.date

is_time(field_name)[source]

Ask conversion of field to an instance of datetime.date

open()[source]

Open an existing database.

update(record, **kw)[source]

Update the record with new keys and values.

PyDbLite.common API

class pydblite.common.Filter(db, key)[source]

A filter to be used to filter the results from a database query. Users should not have to use this class.

__and__(other_filter)[source]

Returns a new filter that combines this filter with other_filter using AND.

__eq__(value)[source]

Perform EQUALS operation When input value is an iterable, but not a string, it will match for any of the values on the iterable

__ge__(value)[source]

Perform GREATER THAN OR EQUALS operation

__gt__(value)[source]

Perform GREATER THAN operation

__iter__()[source]

Returns in iterator over the records for this filter

__le__(value)[source]

Perform LESS THAN OR EQUALS operation

__len__()[source]

Returns the number of records that matches this filter

__lt__(value)[source]

Perform LESS THAN operation

__ne__(value)[source]

Perform NOT EQUALS operation

__or__(other_filter)[source]

Returns a new filter that combines this filter with other_filter using OR.

__str__()[source]

Returns a string representation of the filter

filter()[source]

Returns the filter

ilike(value)[source]

Perform ILIKE operation

is_filtered()[source]

If the filter contains any filters

like(value)[source]

Perform LIKE operation