Some headway on database stuff

This commit is contained in:
Bradley Bickford 2025-10-14 20:41:37 -04:00
parent a30961f884
commit 16dc75a57d
7 changed files with 34 additions and 4 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*__pycache__*

0
test/__init__.py Normal file
View File

29
test/db_interface_test.py Normal file
View File

@ -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"))

BIN
testdb.db Normal file

Binary file not shown.

View File

View File

@ -35,13 +35,13 @@ class Database():
return self.query(query_string, values)[0] 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( query_string = "DELETE FROM {table}{where}".format(
table = table, table = table,
where = self.__generate_basic_where_clause(where) if not where is None else "" 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]): def __generate_basic_where_clause(self, where: list[dict]):
return " WHERE {clauses}".format( return " WHERE {clauses}".format(

View File

@ -1,9 +1,9 @@
from database import Database from .database import Database
import sqlite3 import sqlite3
class SQLite(Database): class SQLite(Database):
def __init__(self, db_name: str): def __init__(self, db_name: str):
super(self, Database).__init__() super(Database, self).__init__()
self.db = sqlite3.connect(db_name) self.db = sqlite3.connect(db_name)
self.db.autocommit = True self.db.autocommit = True