Raw

Raw provides methods for executing raw SQL queries, creating and restoring database backups, listing tables, and handling base64 encoded data.

class DbUnify.SQLite3.sync.Raw.Raw(manager)[source]

Bases: object

# Raw Class

#### The Raw class provides methods for executing raw SQL queries, creating database backups, restoring backups, #### listing tables, inserting and reading base64 encoded data.

### Attributes:
  • manager (Manager): The Manager instance managing the database connection.

### Methods:
  • __init__(self, manager): Initializes the Raw instance with a Manager instance.

  • backup_database(self, backup_path): Creates a backup of the database.

  • restore_database(self, backup_path): Restores the database from a backup.

  • execute_query(self, query, *args): Executes a database query.

  • list_tables(self): Gets a list of all tables in the SQLite database.

  • insert_base64(self, table_name, data_dict): Inserts base64 encoded data into a database table.

  • read_base64(self, table_name, only_base64): Reads and decodes base64 encoded data from a database table.

### Raises:
  • RuntimeError: If there is an error during database backup, restoration, query execution, listing tables, or reading and decoding base64 data.

### Note:
  • This class is designed for asynchronous usage and requires the use of the ‘async’ and ‘await’ keywords for method calls.

  • The ‘Manager’ class is used internally for managing the database connection.

backup_database(backup_path: str) bool[source]

Create a backup of the database.

Parameters:

backup_path (str) – The path where the backup should be stored.

Returns:

True if the backup was successful, False otherwise.

Return type:

bool

Raises:

RuntimeError – If there is an error creating the database backup.

execute_query(query: str, *args: Any) bool[source]

Execute a database query.

Parameters:
  • query (str) – The SQL query to be executed.

  • *args – Parameters to be passed to the query.

Returns:

True if the query was successful, False otherwise.

Return type:

bool

Raises:

RuntimeError – If there is an error executing the query.

insert_base64(table_name: str, data_dict: Dict[str, Any]) None[source]

Insert base64 encoded data into a database table.

Parameters:
  • table_name (str) – Name of the table to insert data into.

  • data_dict (dict) – A dictionary where keys are column names, and values are data to be encoded and inserted.

Raises:

RuntimeError – If there is an error inserting the data.

list_tables() List[str][source]

Get a list of all tables in the SQLite database.

Returns:

A list of table names.

Return type:

list

Raises:

RuntimeError – If there is an error listing tables.

read_base64(table_name: str, only_base64: bool) List[Dict[str, Any]][source]

Read and decode base64 encoded data from a database table.

Parameters:
  • table_name (str) – Name of the table to read data from.

  • only_base64 (bool) – If True, only return rows where at least one column contains base64 encoded data.

Returns:

A list of dictionaries where keys are column names, and values are decoded data as bytes.

Return type:

list

Raises:

RuntimeError – If there is an error selecting or decoding the data.

restore_database(backup_path: str) bool[source]

Restore the database from a backup.

Parameters:

backup_path (str) – The path to the backup file.

Returns:

True if the restore was successful, False otherwise.

Return type:

bool

Raises:

RuntimeError – If there is an error restoring the database.

Attributes:

  • manager (Manager): The Manager instance managing the database connection.

Methods:

__init__

Initialize the Raw instance with a Manager instance.

from DbUnify.SQLite3.sync.Raw import Raw
from DbUnify.SQLite3.sync.Manager import Manager

# Initialize Manager and Raw
manager = Manager(db_name='example.db')
raw = Raw(manager=manager)

backup_database

Create a backup of the database.

from DbUnify.SQLite3.sync.Raw import Raw
from DbUnify.SQLite3.sync.Manager import Manager

# Initialize Manager and Raw
manager = Manager(db_name='example.db')
raw = Raw(manager=manager)

# Backup the database
success = raw.backup_database(backup_path='backups/example_backup.db')
if success:
    print("Backup successful")
else:
    print("Backup failed")

restore_database

Restore the database from a backup.

from DbUnify.SQLite3.sync.Raw import Raw
from DbUnify.SQLite3.sync.Manager import Manager

# Initialize Manager and Raw
manager = Manager(db_name='example.db')
raw = Raw(manager=manager)

# Restore the database from a backup
success = raw.restore_database(backup_path='backups/example_backup.db')
if success:
    print("Restore successful")
else:
    print("Restore failed")

execute_query

Execute a database query.

from DbUnify.SQLite3.sync.Raw import Raw
from DbUnify.SQLite3.sync.Manager import Manager

# Initialize Manager and Raw
manager = Manager(db_name='example.db')
raw = Raw(manager=manager)

# Execute a query to create a table
success = raw.execute_query('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)')
if success:
    print("Table created successfully")
else:
    print("Failed to create table")

list_tables

Get a list of all tables in the SQLite database.

from DbUnify.SQLite3.sync.Raw import Raw
from DbUnify.SQLite3.sync.Manager import Manager

# Initialize Manager and Raw
manager = Manager(db_name='example.db')
raw = Raw(manager=manager)

# List tables in the database
tables = raw.list_tables()
print("Tables:", tables)

insert_base64

Insert base64 encoded data into a database table.

from DbUnify.SQLite3.sync.Raw import Raw
from DbUnify.SQLite3.sync.Manager import Manager

# Initialize Manager and Raw
manager = Manager(db_name='example.db')
raw = Raw(manager=manager)

# Data to be inserted
data = {
    'file_data': 'some binary data',
}

# Insert base64 encoded data
try:
    raw.insert_base64(table_name='files', data_dict=data)
    print("Data inserted successfully")
except RuntimeError as e:
    print(f"Error inserting data: {e}")

read_base64

Read and decode base64 encoded data from a database table.

from DbUnify.SQLite3.sync.Raw import Raw
from DbUnify.SQLite3.sync.Manager import Manager

# Initialize Manager and Raw
manager = Manager(db_name='example.db')
raw = Raw(manager=manager)

# Read base64 encoded data
try:
    rows = raw.read_base64(table_name='files', only_base64=True)
    for row in rows:
        print("Decoded data:", row)
except RuntimeError as e:
    print(f"Error reading data: {e}")