File size: 1,125 Bytes
c5322fc
 
 
 
 
 
 
 
 
 
 
f036750
 
 
c5322fc
f036750
c5322fc
 
 
 
 
 
 
 
 
 
f036750
c5322fc
 
 
 
 
 
 
 
 
 
f036750
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# database/db_manager.py
import mysql.connector
from config import Config
from datetime import datetime

class DatabaseManager:
    def __init__(self):
        self.config = {
            'host': Config.MYSQL_HOST,
            'user': Config.MYSQL_USER,
            'password': Config.MYSQL_PASSWORD,
            'database': Config.MYSQL_DB,
            'port': 3306,
            'auth_plugin': 'mysql_native_password'
        }
    
    def save_message(self, role, content):
        conn = mysql.connector.connect(**self.config)
        cursor = conn.cursor()
        
        query = "INSERT INTO chat_messages (role, content) VALUES (%s, %s)"
        cursor.execute(query, (role, content))
        
        conn.commit()
        cursor.close()
        conn.close()
    
    def get_chat_history(self):
        conn = mysql.connector.connect(**self.config)
        cursor = conn.cursor(dictionary=True)
        
        query = "SELECT * FROM chat_messages ORDER BY timestamp ASC"
        cursor.execute(query)
        history = cursor.fetchall()
        
        cursor.close()
        conn.close()
        return history