From 16dc75a57def54409a0814cc85386efef2454bf8 Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Tue, 14 Oct 2025 20:41:37 -0400 Subject: [PATCH] Some headway on database stuff --- .gitignore | 1 + test/__init__.py | 0 test/db_interface_test.py | 29 +++++++++++++++++++++++++++++ testdb.db | Bin 0 -> 12288 bytes utilities/database/__init__.py | 0 utilities/database/database.py | 4 ++-- utilities/database/sqlite.py | 4 ++-- 7 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 test/__init__.py create mode 100644 test/db_interface_test.py create mode 100644 testdb.db create mode 100644 utilities/database/__init__.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..abd32e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*__pycache__* \ No newline at end of file diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/db_interface_test.py b/test/db_interface_test.py new file mode 100644 index 0000000..1c9cfa4 --- /dev/null +++ b/test/db_interface_test.py @@ -0,0 +1,29 @@ +from utilities.database.sqlite import SQLite +from pprint import pprint + +test_db = SQLite("testdb.db") + +test_db.query("CREATE TABLE vegetables (veggie_id INTEGER PRIMARY KEY AUTOINCREMENT, veggie_name TEXT NOT NULL)") + +test_db.insert("vegetables", ["veggie_name"], ["Green Beans"]) +test_db.insert("vegetables", ["veggie_name"], ["Peas"]) +test_db.insert("vegetables", ["veggie_name"], ["Corn"]) +test_db.insert("vegetables", ["veggie_name"], ["Stephen Hawking"]) + +pprint(test_db.select("vegetables")) + +pprint(test_db.update("vegetables", ["veggie_name"], ["Snow Peas", 2], [ + { + "name": "veggie_id", + "compare": "=" + } +])) + +pprint(test_db.delete("vegetables", [3], [ + { + "name": "veggie_id", + "compare": ">=" + } +])) + +pprint(test_db.select("vegetables")) diff --git a/testdb.db b/testdb.db new file mode 100644 index 0000000000000000000000000000000000000000..9ed119504c0e1e70c42792ed4ca928d26636a018 GIT binary patch literal 12288 zcmeI&!A`PC0SG_<0uX=z1Rwwb2tWV=5ZGyfmbRBGm1H;e?*}Rx54dga zMQZ$}!iVjtUUen`0SG_<0uX=z1Rwwb2tWV=5P-l=2^?s;IBAEY8MTxj@vEJc+Oc|lRv}&c zvzH+3^V)H?I+}!>>}QjzL#d0?+C*_pMfqWre4CfXiu->~5D1Rwwb2tWV=5P$## TAOHaf{0jl@|MPP5A3^F9dp>J2 literal 0 HcmV?d00001 diff --git a/utilities/database/__init__.py b/utilities/database/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utilities/database/database.py b/utilities/database/database.py index 28d4794..838dded 100644 --- a/utilities/database/database.py +++ b/utilities/database/database.py @@ -35,13 +35,13 @@ class Database(): return self.query(query_string, values)[0] - def delete(self, table: str, where: list[dict]=None) -> int: + def delete(self, table: str, values: list, where: list[dict]=None) -> int: query_string = "DELETE FROM {table}{where}".format( table = table, where = self.__generate_basic_where_clause(where) if not where is None else "" ) - return self.query(query_string)[0] + return self.query(query_string, values)[0] def __generate_basic_where_clause(self, where: list[dict]): return " WHERE {clauses}".format( diff --git a/utilities/database/sqlite.py b/utilities/database/sqlite.py index d147885..28bd927 100644 --- a/utilities/database/sqlite.py +++ b/utilities/database/sqlite.py @@ -1,9 +1,9 @@ -from database import Database +from .database import Database import sqlite3 class SQLite(Database): def __init__(self, db_name: str): - super(self, Database).__init__() + super(Database, self).__init__() self.db = sqlite3.connect(db_name) self.db.autocommit = True