Database class

from database import DataBase

Creates a database backed by a file on flash storage

Constants

database.VER

module version. Currently 2.0

Constructor

def __init__(self, filename='upy.db')

Create a database. filename - file used as a storage for the database. Be careful not to use one file for two databases. A global instance of a database class named udb is created in the config modules. This instance uses the default upy.db file.

Clean database

def clean(self)

Remote all entries from the database

Add an entry

def add(self, uid, data):

Add an entry identified by uid and containing data to the database.

Accepted data types: bytes, str

  • uid - database key (a str object)

Remove an entry

def remove(self, uid)

Remove an entry identified by uid from the database.

Retrieve an entry

def get(self, uid)

Get the data identified by uid from the database. Data is returned as a bytes object

Take an entry

def pop(self, uid)

Get an entry identified by uid, remove it from the database and return it.

Search the database

def contains(self, uid)

Return True/False if an entry identified by uid is / is not in the database.

__contains__ function is also supported since version 2.0. Example

>>>from database import DataBase
>>>db = DataBase('test.db')
>>>db.add('hello', 'world')
>>>'hello' in db
True
>>>'bye' in db
False

Check if the database is empty

def empty(self)