diff --git a/jarvis/db/__init__.py b/jarvis/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/jarvis/db/create_empty_database.sql b/jarvis/db/create_empty_database.sql new file mode 100644 index 0000000..28a161c --- /dev/null +++ b/jarvis/db/create_empty_database.sql @@ -0,0 +1,14 @@ +--- Create a commands table that contains all the already executed commandes and the simplified version by ChatGPT +DROP TABLE IF EXISTS queries; +CREATE TABLE queries ( + id INTEGER PRIMARY KEY, + original_command VARCHAR NOT NULL, + simplified_command VARCHAR NOT NULL, + response INT NOT NULL +); + +DROP TABLE IF EXISTS responses: +CREATE TABLE responses ( + id INTEGER PRIMARY KEY, + response VARCHAR NOT NULL +); \ No newline at end of file diff --git a/jarvis/db/db_utils.py b/jarvis/db/db_utils.py new file mode 100644 index 0000000..9ef3283 --- /dev/null +++ b/jarvis/db/db_utils.py @@ -0,0 +1,39 @@ +import sqlite3 +from pathlib import Path + + +def create_database(): + """Creates the database.""" + + with open(Path(__file__).parent / "create_empty_database.sql", "r") as create_database_script: + create_database_script = create_database_script.read() + + db = sqlite3.connect(Path(__file__).parent / "jarvis-commands-memory.sqlite") + cursor = db.cursor() + cursor.executescript(create_database_script) + db.commit() + db.close() + + +def do_request(request, args=None): + """Execute SQL request and return result""" + + conn = sqlite3.connect(Path(__file__).parent / "jarvis-commands-memory.sqlite") + cursor = conn.cursor() + + if not args: + cursor.execute(request) + else: + cursor.execute(request, args) + + conn.commit() + result = cursor.fetchall() + conn.close() + return result + + +def add_query(original_query, simplified_query, response): + """Add a query to the database""" + + do_request("INSERT INTO queries (original_query, simplified_query, response) VALUES (?, ?, ?)", + (original_query, simplified_query, response))