started creating database for local use
This commit is contained in:
parent
ae0014fd6c
commit
22bb680dca
0
jarvis/db/__init__.py
Normal file
0
jarvis/db/__init__.py
Normal file
14
jarvis/db/create_empty_database.sql
Normal file
14
jarvis/db/create_empty_database.sql
Normal 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
39
jarvis/db/db_utils.py
Normal 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))
|
Loading…
Reference in New Issue
Block a user