started creating database for local use

This commit is contained in:
Mathieu Broillet 2023-05-31 18:53:14 +02:00
parent ae0014fd6c
commit 22bb680dca
3 changed files with 53 additions and 0 deletions

0
jarvis/db/__init__.py Normal file
View File

View File

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

39
jarvis/db/db_utils.py Normal file
View File

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