db_manager.py

mail@pastecode.io avatar
unknown
python
8 days ago
997 B
3
Indexable
Never
import sqlite3
import settings as cfg
import uuid

KNOWLEDGE_DB = cfg.KNOWLEDGE_DB

def init_db():
    conn = sqlite3.connect(KNOWLEDGE_DB)
    cursor = conn.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS knowledge_base (
            id TEXT PRIMARY KEY,
            question TEXT NOT NULL,
            answer TEXT NOT NULL
        )
    ''')
    conn.commit()
    conn.close()

def add_knowledge(question, answer):
    conn = sqlite3.connect(KNOWLEDGE_DB)
    cursor = conn.cursor()
    # Erzeugen einer neuen GUID für den Datensatz
    new_id = str(uuid.uuid4())
    cursor.execute('INSERT INTO knowledge_base (id, question, answer) VALUES (?, ?, ?)', (new_id, question, answer))
    conn.commit()
    conn.close()
    return new_id

def fetch_knowledge():
    conn = sqlite3.connect(KNOWLEDGE_DB)
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM knowledge_base')
    rows = cursor.fetchall()
    conn.close()
    return rows
Leave a Comment