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
-
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
-
fields
= None¶ The list of the fields (does not include the internal fields __id__ and __version__)
-
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
-
path
= None¶ The path of the database in the file system
-
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.
-
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
-
__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)
-
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.
-
__init__
(table_name, db)[source]¶ Args:
- table_name (str): The name of the SQLite table.
- db (Database): The database.
-
_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)
-
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
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
-