File size: 2,189 Bytes
6340cf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sqlite3
import pandas as pd
import logging
from create_course_dataframe import create_course_dataframe, process_xlsx 

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(message)s",
    handlers=[logging.StreamHandler()]
)

def save_to_sqlite(df, database_name='Spring_2025_courses.db', table_name='Spring_2025_courses'):
    """
    Save DataFrame to SQLite Database
    """
    try:
        logging.info(f"Connecting to database: {database_name}")
        conn = sqlite3.connect(database_name)
        
        logging.info(f"Saving DataFrame to table: {table_name}")
        df.to_sql(table_name, conn, if_exists='replace', index=False)
        
        logging.info("Data successfully saved to SQLite database")
        
        # Verify the data
        row_count = pd.read_sql(f"SELECT COUNT(*) FROM {table_name}", conn).iloc[0,0]
        logging.info(f"Verified {row_count} rows in table {table_name}")
        
        conn.close()
        return True
        
    except Exception as e:
        logging.error(f"Error saving to database: {str(e)}")
        return False

if __name__ == "__main__":
    # Load and process XLSX file
    file_path = "data/FTCM_Course_List_Spring2025.xlsx"
    result = process_xlsx(file_path)

    if result:
        column_names, department_program_courses = result
        modified_column_names = [name.replace(' ', '').replace('\n', '') if name else name for name in column_names]
        cleaned_column_names = ['CourseCode', 'CourseTitle', 'Cr', 'Prereq(s)', 
            'Instructor', 'Major/GE/Elective', 'Format', 'Mon', 'MonTo',
            'Tue', 'TueTo', 'Wed', 'WedTo', 'Thu', 'ThuTo', 
            'Fri', 'FriTo', 'Sat', 'SatTo', 'Platform', 'New/Repeat', 'Room', 'Department', 'Program']
        
        # Create DataFrame
        df = create_course_dataframe(cleaned_column_names, modified_column_names, department_program_courses)
        
        # Save to SQLite
        if save_to_sqlite(df):
            logging.info("Process completed successfully")
        else:
            logging.error("Failed to save data to SQLite")
    else:
        logging.error("Failed to process XLSX file")