Spaces:
Paused
Paused
Update utils/database.py
Browse files- utils/database.py +43 -2
utils/database.py
CHANGED
|
@@ -35,6 +35,27 @@ def create_connection(db_file):
|
|
| 35 |
st.error("Failed to connect to database. Please try again or contact support.")
|
| 36 |
return None
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def create_tables(conn):
|
| 39 |
"""Create necessary tables in the database."""
|
| 40 |
try:
|
|
@@ -75,8 +96,28 @@ def create_tables(conn):
|
|
| 75 |
except Error as e:
|
| 76 |
st.error(f"Error: {e}")
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def get_documents(conn):
|
| 81 |
"""Retrieve all documents from the database.
|
| 82 |
|
|
|
|
| 35 |
st.error("Failed to connect to database. Please try again or contact support.")
|
| 36 |
return None
|
| 37 |
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# Add this function to your database.py file
|
| 41 |
+
def get_db_connection():
|
| 42 |
+
"""Get a thread-safe database connection."""
|
| 43 |
+
try:
|
| 44 |
+
data_dir = Path("data")
|
| 45 |
+
data_dir.mkdir(exist_ok=True)
|
| 46 |
+
db_path = data_dir / 'rfp_analysis.db'
|
| 47 |
+
|
| 48 |
+
# Create new connection
|
| 49 |
+
conn = sqlite3.connect(str(db_path))
|
| 50 |
+
|
| 51 |
+
# Create tables if they don't exist
|
| 52 |
+
create_tables(conn)
|
| 53 |
+
|
| 54 |
+
return conn
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.error(f"Database connection error: {str(e)}")
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
def create_tables(conn):
|
| 60 |
"""Create necessary tables in the database."""
|
| 61 |
try:
|
|
|
|
| 96 |
except Error as e:
|
| 97 |
st.error(f"Error: {e}")
|
| 98 |
|
| 99 |
+
def insert_document(name, content):
|
| 100 |
+
"""Insert a document with thread-safe connection."""
|
| 101 |
+
try:
|
| 102 |
+
conn = get_db_connection()
|
| 103 |
+
if conn is None:
|
| 104 |
+
return None
|
| 105 |
+
|
| 106 |
+
cursor = conn.cursor()
|
| 107 |
+
cursor.execute(
|
| 108 |
+
"INSERT INTO documents (name, content) VALUES (?, ?)",
|
| 109 |
+
(name, content)
|
| 110 |
+
)
|
| 111 |
+
conn.commit()
|
| 112 |
+
doc_id = cursor.lastrowid
|
| 113 |
+
conn.close()
|
| 114 |
+
return doc_id
|
| 115 |
+
except Exception as e:
|
| 116 |
+
st.error(f"Error inserting document: {str(e)}")
|
| 117 |
+
if conn:
|
| 118 |
+
conn.rollback()
|
| 119 |
+
conn.close()
|
| 120 |
+
return None
|
| 121 |
def get_documents(conn):
|
| 122 |
"""Retrieve all documents from the database.
|
| 123 |
|