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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: DBbeginners-0.1-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-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 033ecbcda5487c7b12bfd753e04c4fb7ff19073b26362a4182b732f6fe23d241
MD5 2cbaac084909918641b64f3e817cd2e8
BLAKE2b-256 4d511dc160b5acf6a3221033fbe0bc78208a4136f3e9828e19ab4617117e35d5

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