Skip to main content

A lightweight SQLite database utility module

Project description

DataBase Class Documentation The DataBase class is designed to interact with SQLite databases, providing methods for creating tables, inserting data, querying data, updating data, deleting data, and managing transactions. It also offers utility methods for getting affected rows count, table columns, and table rows count.

Table of Contents

def init(self, db_name: str, table_name: str, columns: List[str]): self.db_name = db_name self.table_name = table_name self.columns = columns self.conn = None

Initializes the DataBase object with the given database name, table name, and column names. The connection to the database is established when needed.

Connecting and Closing Database def connect(self) -> None: # ...

def close(self) -> None: # ...

The connect method establishes a connection to the database if it's not already connected. The close method closes the database connection if it's open.

Creating Table def create_table(self) -> None: # ...

Creates a table in the database with the specified name and columns. If the table already exists, it does nothing.

Inserting Data def insert_data(self, data: List[Tuple]) -> None: # ...

Inserts the given data into the table. The data should be a list of tuples, where each tuple represents a row of data.

Querying Data def select_data(self, query: str) -> List[Tuple]: # ...

Executes the given SQL query and returns the result as a list of tuples. If the query is invalid or fails to execute, it returns an empty list.

Updating Data def update_data(self, query: str) -> None: # ...

Executes the given SQL query to update data in the table. If the query is invalid or fails to execute, it prints an error message.

Deleting Data def delete_data(self, query: str) -> None: # ...

Executes the given SQL query to delete data from the table. If the query is invalid or fails to execute, it prints an error message.

Transaction Management def begin_transaction(self) -> None: # ...

def commit_transaction(self) -> None: # ...

def rollback_transaction(self) -> None: # ...

The begin_transaction method starts a new transaction. The commit_transaction method commits the current transaction. The rollback_transaction method rolls back the current transaction.

Executing Raw Query def execute_raw_query(self, query: str) -> None: # ...

Executes the given raw SQL query and commits the transaction. If the query is invalid or fails to execute, it prints an error message.

Getting Affected Rows Count def get_affected_rows(self) -> int: # ...

Returns the number of rows affected by the last SQL statement.

Getting Table Columns def get_table_columns(self) -> List[str]: # ...

Returns a list of column names in the table. If there's an error retrieving the columns, it returns an empty list.

Getting Table Rows Count def get_table_rows_count(self) -> int: # ...

Returns the number of rows in the table. If there's an error retrieving the row count, it returns 0.

Example Usage db = DataBase('example.db', 'users', ['id', 'name', 'email'])

Create table

db.create_table()

Insert data

data = [(1, 'John Doe', 'john.doe@example.com'), (2, 'Jane Smith', 'jane.smith@example.com')] db.insert_data(data)

Query data

result = db.select_data('SELECT * FROM users') print(result)

Update data

db.update_data('UPDATE users SET email = "john.doe.updated@example.com" WHERE id = 1')

Delete data

db.delete_data('DELETE FROM users WHERE id = 2')

Get affected rows count

affected_rows = db.get_affected_rows() print(f"Affected rows: {affected_rows}")

Get table columns

columns = db.get_table_columns() print(f"Table columns: {columns}")

Get table rows count

rows_count = db.get_table_rows_count() print(f"Table rows count: {rows_count}")

Close the database connection

db.close()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dbbeginners-1.0.0.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

DBbeginners-1.0.0-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

Details for the file dbbeginners-1.0.0.tar.gz.

File metadata

  • Download URL: dbbeginners-1.0.0.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for dbbeginners-1.0.0.tar.gz
Algorithm Hash digest
SHA256 37aef5863959cb949933755b0ced35b78af70a0af05ff0fd6962d83f3de52809
MD5 79b5003d991dfa3f6108f63b00ec93aa
BLAKE2b-256 e5173904f151a286699c04a2cdeb8fa937391854a4b147d28b40a7258a9a7192

See more details on using hashes here.

File details

Details for the file DBbeginners-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: DBbeginners-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 2.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for DBbeginners-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a0bf8fe481d34ec4971cbe58ebae7eed08b36e381906bb9735ea551b9aa721c6
MD5 1637a7c1fa9daf5e7d58adb7e12219a0
BLAKE2b-256 8e030bce5ab943f6e5db7c38a1b4c1689a7cf63b21afece8ca09a0f43693a0b8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page