File size: 1,164 Bytes
68bcdee
 
 
 
 
 
 
 
f1462f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
# src/utils/__init__.py
from .session_state import initialize_session_state
from .database import get_documents, insert_document

__all__ = [
    'initialize_session_state',
    'get_documents',
    'insert_document'
]

# src/utils/database.py
import streamlit as st
import sqlite3
from datetime import datetime

def get_documents(conn):
    """Retrieve documents from database"""
    try:
        cursor = conn.cursor()
        cursor.execute("SELECT id, name, upload_date FROM documents ORDER BY upload_date DESC")
        return cursor.fetchall()
    except Exception as e:
        st.error(f"Error retrieving documents: {e}")
        return []

def insert_document(conn, doc_name, doc_content):
    """Insert a document into database"""
    try:
        cursor = conn.cursor()
        cursor.execute("SELECT id FROM documents WHERE name = ?", (doc_name,))
        if not cursor.fetchone():
            conn.execute(
                "INSERT INTO documents (name, content) VALUES (?, ?)",
                (doc_name, doc_content)
            )
            return True
    except Exception as e:
        st.error(f"Error inserting document: {e}")
    return False