Database

All databases have a table ‘’meta’’ with the following scheme:

id

key

value

key:

A string to identify a meta data variable

value:

The value to a key as string

One important key is ‘’version’ that allows to identify old schemes and allows an easy update of the database. There may be other keys depending on the databases. In that case they are described in the documentations of the related modules.

Database Class

class musicdb.lib.db.database.Database(path)[source]

This is the base class for all database classes in MusicDB. It establishes the connection to the sqlite3 databases.

The connection timeout is set to 20.

Parameters:

path (str) – absolute path to the database file.

Raises:

TypeError – When path is not a string

Compress(string)[source]

This method can be used to compress a string before storing it into the database.

To keep a consistent database, use only this method and the complementary method Decompress(). It uses gzip for compression.

Parameters:

string (str) – A string to compress

Returns:

A byte blob with the compresses string

Decompress(blob)[source]

This method can be used to decompress a compresses string after reading it from the database.

To keep a consistent database, use only this method and the complementary method Compress(). It uses gzip for decompression.

Parameters:

blob – A byte blob with the compresses string

Returns:

The decompresses string

Execute(sql, values=None)[source]

This method executes a SQL command. When the command fails, the database gets rolled back. Otherwise the changes gets committed.

values can be a single value, a list, a dictionary or a tuple.

Parameters:
  • sql (str) – SQL command

  • values – Optional arguments used in the command

Returns:

None

Raises:

TypeError – When sql is not a string

Examples

db = Database("test.db")

sql    = "INSERT INTO valuetable (name, content) VALUES (?, ?)"
values = ("Name", 1000)

db.Execute(sql, values)
db = Database("test.db")

sql    = "UPDATE valuetable SET content = ? WHERE name = ?"
values = (1000, "Name")

db.Execute(sql, values)
ExecuteScript(sqlscript)[source]

This method executes multiple SQL commands stored in sqlscript. When the command fails, the database gets rolled back. Otherwise the changes gets committed.

Parameters:

sqlscript (str) – SQL script

Returns:

None

Raises:

TypeError – When sqlscript is not a string

GetFromDatabase(sql, values=None)[source]

This method gets values from the database by executing a SQL command and fetching the results. When the command fails, the database gets rolled back.

values can be a single value, a list, a dictionary or a tuple.

This method returns a list of tuples. Each list element corresponds to one returned row. Each row is represented by a tuple of values.

If only one value is expected, it is [(x,)].

Parameters:
  • sql (str) – SQL command

  • values – Optional arguments used in the command

Returns:

The results of the SQL command

Raises:

TypeError – When sql is not a string

Example

db = Database("test.db")

sql = "SELECT name FROM valuetable WHERE content = ?"

results = db.Execute(sql, 1000)
if not results:
    print("No entries with value 1000 in database")
    return

for entry in results:
    print(entry[0])