40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
|
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))
|