My database code is pretty simple
import mysql.connector
import datetime
import streamlit as st
config = {
‘user’: st.secrets[‘Main_Database’][‘username’],
‘password’: st.secrets[‘Main_Database’][‘password’],
‘host’: st.secrets[‘Main_Database’][‘host’],
‘database’: st.secrets[‘Main_Database’][‘database’],
‘port’ : st.secrets[‘Main_Database’][‘port’],
‘raise_on_warnings’: True,
‘connection_timeout’: 10
}
conn = mysql.connector.connect(**config)
cursor = conn.cursor()
def get_branches():
cursor.execute(“SELECT Branch_Name, Id FROM branch”)
branches = [(row[0], row[1]) for row in cursor.fetchall()] # ← Use cursor.fetchall()
return branches
def get_sems(branch_id):
cursor.execute(“SELECT Sem_Name, Id FROM sem WHERE Branch_Id = %s”, (branch_id,))
sems = [(row[0], row[1]) for row in cursor.fetchall()] # ← Use cursor.fetchall()
return sems
def get_subjects(sem_id):
cursor.execute(“SELECT Subject_Name, Id FROM subject WHERE Sem_Id = %s”, (sem_id,))
subjects = [(row[0], row[1]) for row in cursor.fetchall()] # ← Use cursor.fetchall()
return subjects
def get_chapters(subject_id):
cursor.execute(“SELECT Chapter_Name, Id FROM chapter WHERE Subject_Id = %s”, (subject_id,))
chapters = [{“Chapter_Name”: row[0], “Chapter_Id”: row[1]} for row in cursor.fetchall()] # ← Use cursor.fetchall()
return chapters
def get_pdf_link(chapter_id):
cursor.execute(“SELECT Link FROM chapter WHERE Id = %s”, (chapter_id,))
pdf_link = cursor.fetchone()
if pdf_link is not None:
return pdf_link[0]
else:
print(f"No link found for chapter ID: {chapter_id}")
return None
def insert_record(user_messages, bot_responses, chapter_id):
cursor.execute(“INSERT INTO chat (Question, Response, Chapter_Id, Created_At, Updated_At) VALUES (%s, %s, %s, %s, %s)”,
(user_messages, bot_responses, chapter_id, datetime.datetime.now(), datetime.datetime.now()))
conn.commit()
def update_chat(chat_id, like_dislike):
cursor.execute(“UPDATE chat SET Reaction = %s WHERE Id = %s”, (like_dislike, chat_id))
conn.commit()
the database has hardly 5-10 rows, it is still in the testing phase & the mysql db is with godaddy