database improvements
This commit is contained in:
parent
ea477269c9
commit
b7419a6e58
@ -1,13 +1,13 @@
|
||||
--- 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 (
|
||||
DROP TABLE IF EXISTS commands;
|
||||
CREATE TABLE commands (
|
||||
id INTEGER PRIMARY KEY,
|
||||
original_command VARCHAR NOT NULL,
|
||||
simplified_command VARCHAR NOT NULL,
|
||||
response INT NOT NULL
|
||||
);
|
||||
|
||||
DROP TABLE IF EXISTS responses:
|
||||
DROP TABLE IF EXISTS responses;
|
||||
CREATE TABLE responses (
|
||||
id INTEGER PRIMARY KEY,
|
||||
response VARCHAR NOT NULL
|
||||
|
@ -1,3 +1,5 @@
|
||||
import logging
|
||||
import os.path
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
@ -5,6 +7,11 @@ from pathlib import Path
|
||||
def create_database():
|
||||
"""Creates the database."""
|
||||
|
||||
# Check if the database already exists
|
||||
if os.path.exists(Path(__file__).parent / "jarvis-commands-memory.sqlite"):
|
||||
logging.debug("Database already exists, skipping creation")
|
||||
return
|
||||
|
||||
with open(Path(__file__).parent / "create_empty_database.sql", "r") as create_database_script:
|
||||
create_database_script = create_database_script.read()
|
||||
|
||||
@ -14,6 +21,8 @@ def create_database():
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
logging.info("Database created")
|
||||
|
||||
|
||||
def do_request(request, args=None):
|
||||
"""Execute SQL request and return result"""
|
||||
@ -32,8 +41,27 @@ def do_request(request, args=None):
|
||||
return result
|
||||
|
||||
|
||||
def add_query(original_query, simplified_query, response):
|
||||
"""Add a query to the database"""
|
||||
def add_response(response):
|
||||
"""Add a response to the database"""
|
||||
|
||||
do_request("INSERT INTO queries (original_query, simplified_query, response) VALUES (?, ?, ?)",
|
||||
(original_query, simplified_query, response))
|
||||
# Check if the response is already in the database
|
||||
request = do_request("SELECT * FROM responses WHERE response=?", (response,))
|
||||
if request:
|
||||
response_id = request[0][0]
|
||||
logging.debug("Response already in database (id: " + str(response_id) + ")")
|
||||
|
||||
# If not, add it
|
||||
else:
|
||||
do_request("INSERT INTO responses (response) VALUES (?)", (response,))
|
||||
response_id = do_request("SELECT * FROM responses WHERE response=?", (response,))[0][0]
|
||||
logging.debug("Response added to database with id " + str(response_id) + ": " + str(response))
|
||||
|
||||
return response_id
|
||||
|
||||
|
||||
def add_command(original_command, simplified_command, response):
|
||||
"""Add a command to the database"""
|
||||
|
||||
do_request("INSERT INTO commands (original_command, simplified_command, response) VALUES (?, ?, ?)",
|
||||
(original_command, simplified_command, add_response(response)))
|
||||
logging.debug("Query added to database: " + original_command + " -> " + response)
|
||||
|
Loading…
Reference in New Issue
Block a user