specialized-agents-api / database.py
pvanand's picture
Create database.py
b6eab24 verified
import sqlite3
import os
from config import DB_PATH
import logging
logger = logging.getLogger(__name__)
def init_db():
logger.info("Initializing database")
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS conversations
(id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT,
conversation_id TEXT,
message TEXT,
response TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
conn.commit()
conn.close()
logger.info("Database initialized successfully")
def update_db(user_id, conversation_id, message, response):
logger.info(f"Updating database for conversation: {conversation_id}")
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('''INSERT INTO conversations (user_id, conversation_id, message, response)
VALUES (?, ?, ?, ?)''', (user_id, conversation_id, message, response))
conn.commit()
conn.close()
logger.info("Database updated successfully")