File size: 1,493 Bytes
e4fe207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# program.py
import streamlit as st
import pandas as pd
from app5_selectbox.database_con import cursor, db_connection
from app5_selectbox.app5_selectbox_func import display_table, generate_unique_4


def program(table_name):
    prog_id = generate_unique_4(cursor, "prog_id", table_name)
    prog_code = st.text_input("Program Code", key="prog_code").upper()
    prog_name = st.text_input("Program Name", key="prog_name").upper()

    if st.button("Insert Program Record"):
        # Check if any field is empty
        if not prog_code or not prog_name:
            st.error("Program Code and Program Name are required. Please provide values for both fields.")
        else:
            try:
                # Check for duplicates
                cursor.execute("SELECT prog_id FROM program WHERE prog_code = %s", (prog_code,))
                result = cursor.fetchone()
                if result is not None:
                    st.error("A program with the same program code already exists.")
                else:
                    # Insert a record into the program table
                    cursor.execute("INSERT INTO program (prog_id, prog_code, prog_name) VALUES (%s, %s, %s)",
                                   (prog_id, prog_code, prog_name))
                    db_connection.commit()
                    st.success("Record inserted successfully.")
            except Exception as e:
                st.error(f"An error occurred: {str(e)}")
    display_table(cursor, table_name)