- Download Python 3.7+ from python.org
- Ensure Python is added to your system PATH
- Download and install MySQL from mysql.com
- Or use alternatives like XAMPP, WAMP, or cloud services like AWS RDS
pip install mysql-connector-python
First, create your database and tables. Here's the SQL schema:
-- Create database
CREATE DATABASE school_management;
USE school_management;
-- Create tables
CREATE TABLE parents (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
phone VARCHAR(20),
email VARCHAR(100)
);
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
grade VARCHAR(20),
email VARCHAR(100),
parent_id INT,
FOREIGN KEY (parent_id) REFERENCES parents(id)
);
CREATE TABLE subjects (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
department_id INT
);
CREATE TABLE teachers (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
subject_id INT,
email VARCHAR(100),
FOREIGN KEY (subject_id) REFERENCES subjects(id)
);
CREATE TABLE classrooms (
id INT PRIMARY KEY,
room_number VARCHAR(20) NOT NULL,
capacity INT
);
CREATE TABLE classes (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
teacher_id INT,
classroom_id INT,
FOREIGN KEY (teacher_id) REFERENCES teachers(id),
FOREIGN KEY (classroom_id) REFERENCES classrooms(id)
);
CREATE TABLE enrollments (
student_id INT,
class_id INT,
PRIMARY KEY (student_id, class_id),
FOREIGN KEY (student_id) REFERENCES students(id),
FOREIGN KEY (class_id) REFERENCES classes(id)
);
CREATE TABLE grades (
student_id INT,
class_id INT,
grade VARCHAR(5),
PRIMARY KEY (student_id, class_id),
FOREIGN KEY (student_id) REFERENCES students(id),
FOREIGN KEY (class_id) REFERENCES classes(id)
);
CREATE TABLE attendance (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT,
class_id INT,
date DATE,
status ENUM('Present', 'Absent'),
FOREIGN KEY (student_id) REFERENCES students(id),
FOREIGN KEY (class_id) REFERENCES classes(id)
);
In the main()
function, update the DB_CONFIG
dictionary with your actual database credentials:
DB_CONFIG = {
'host': 'localhost', # Your MySQL host (usually localhost)
'user': 'your_mysql_username', # Your MySQL username (e.g., 'root')
'password': 'your_password', # Your MySQL password
'database': 'school_management' # Database name you created
}
DB_CONFIG = {
'host': 'localhost',
'user': 'root',
'password': 'mypassword123',
'database': 'school_management'
}
- Save the Python code as
school_management.py
- Open terminal/command prompt
- Navigate to the file directory:
cd path/to/your/file
- Run the program:
python school_management.py
- Open the file in your preferred IDE (VS Code, PyCharm, etc.)
- Update the database configuration
- Run the file directly from your IDE
Since students reference parents, add parents first:
- Choose option 2 (Insert data)
- Select 9 (Parents)
- Add parent information
- Add subjects (option 3)
- Add classrooms (option 5)
- Teachers reference subjects, so add them after subjects
- Classes reference teachers and classrooms
- Students reference parents
- These reference students and classes
========================================
SCHOOL MANAGEMENT SYSTEM
========================================
1. View data from a table
2. Insert data into a table
3. Update a record
4. Modify table attributes (add/drop column)
5. Exit
----------------------------------------
Enter your choice (1-5): 2
Select the table you want to insert data into:
1. Students
2. Teachers
3. Subjects
4. Classes
5. Classrooms
6. Enrollments
7. Grades
8. Attendance
9. Parents
Enter your choice (1-9): 9
Enter parent ID: 1
Enter parent name: John Smith
Enter phone number: +1234567890
Enter email address: [email protected]
-
"No module named 'mysql.connector'"
pip install mysql-connector-python
-
"Access denied for user"
- Check your MySQL username and password
- Ensure MySQL service is running
- Grant proper privileges to your MySQL user
-
"Unknown database"
- Make sure you've created the database
- Check database name spelling
-
"Table doesn't exist"
- Run the SQL schema to create all tables
- Check table names match exactly
-
Foreign key constraint errors
- Insert data in the correct order (parents before students, etc.)
- Ensure referenced IDs exist
You can test your database connection separately:
import mysql.connector
try:
connection = mysql.connector.connect(
host='localhost',
user='your_username',
password='your_password',
database='school_management'
)
print("Connection successful!")
connection.close()
except Exception as e:
print(f"Connection failed: {e}")
- Never hardcode passwords in production
- Use environment variables or config files for sensitive data
- Consider using connection pooling for production applications
- Implement proper user authentication and authorization
Once running successfully, you can:
- Add more validation rules
- Implement data export features
- Add reporting capabilities
- Create a web interface
- Add backup and restore functionality