0% found this document useful (0 votes)
3 views11 pages

Farooq Shahzad 70126800

The document outlines a PyQt5 application for managing student data, including functionalities for adding, editing, deleting, and displaying records in a SQLite database. It includes form validation for input fields such as name, age, email, and contact number. The application features a user interface with a table to display student records and buttons for user interactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Farooq Shahzad 70126800

The document outlines a PyQt5 application for managing student data, including functionalities for adding, editing, deleting, and displaying records in a SQLite database. It includes form validation for input fields such as name, age, email, and contact number. The application features a user interface with a table to display student records and buttons for user interactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Farooq Shahzaf: 70126800 | M.

Sohaib: 70126364
Adv Se Project Part 1

import sys
import sqlite3
import re
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout,
QHBoxLayout, QFormLayout, QLineEdit, QComboBox, QPushButton,
QTableWidget, QTableWidgetItem, QHeaderView, QMessageBox,
QTextEdit, QSpinBox, QDateEdit
from PyQt5.QtCore import Qt, QDate

# Database setup
def initialize_db():
conn = sqlite3.connect("student_data.db")
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS student_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
age INTEGER,
address TEXT,
contact_number TEXT,
email TEXT,
gender TEXT,
dob TEXT,
course TEXT,
year INTEGER,
feedback TEXT
)
''')
conn.commit()
conn.close()

# Function to validate email format


def is_valid_email(email):
regex = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
return re.match(regex, email) is not None

# Refresh table function


def refresh_table():
"""Refresh the data displayed in the table."""
conn = sqlite3.connect("student_data.db")
cursor = conn.cursor()

# Clear current table contents


table.setRowCount(0)

cursor.execute("SELECT * FROM student_data")


rows = cursor.fetchall()

# Insert fetched data into the table


for row in rows:
row_position = table.rowCount()
table.insertRow(row_position)
for i, val in enumerate(row):
table.setItem(row_position, i, QTableWidgetItem(str(val)))

conn.close()

# Function to submit form


def submit_form():
"""Submit the form data to the database."""
name = name_input.text()
age = age_input.text()
address = address_input.text()
contact_number = contact_number_input.text()
email = email_input.text()
gender = gender_input.currentText()
dob = dob_input.text()
course = course_input.text()
year = year_input.text()
feedback = feedback_input.toPlainText()

# Validation checks
if not all([name, age, address, contact_number, email, gender, dob,
course, year, feedback]):
QMessageBox.warning(window, "Validation Error", "All fields must
be filled!")
return

if not name.isalpha():
QMessageBox.warning(window, "Validation Error", "Name must
contain only letters!")
return

if not age.isdigit() or not (1 <= int(age) <= 100):


QMessageBox.warning(window, "Input Error", "Age must be a
number between 1 and 100!")
return

if not re.match(r'^\d{10}$', contact_number):


QMessageBox.warning(window, "Input Error", "Contact number
must be a 10-digit number!")
return
if not is_valid_email(email):
QMessageBox.warning(window, "Input Error", "Invalid email
format!")
return

if not dob:
QMessageBox.warning(window, "Input Error", "Date of Birth is
required!")
return

if not year.isdigit() or not (1 <= int(year) <= 4):


QMessageBox.warning(window, "Input Error", "Year must be a
number between 1 and 4!")
return

# Insert data into the database


conn = sqlite3.connect("student_data.db")
cursor = conn.cursor()
cursor.execute('''
INSERT INTO student_data (name, age, address, contact_number,
email, gender, dob, course, year, feedback)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (name, age, address, contact_number, email, gender, dob, course,
year, feedback))
conn.commit()
conn.close()

# Refresh table and clear form


refresh_table()
clear_form()

# Clear form fields


def clear_form():
name_input.clear()
age_input.clear()
address_input.clear()
contact_number_input.clear()
email_input.clear()
gender_input.setCurrentIndex(0)
dob_input.clear()
course_input.clear()
year_input.clear()
feedback_input.clear()

# Delete selected row


def delete_selected():
selected_row = table.currentRow()
if selected_row < 0:
QMessageBox.warning(window, "Selection Error", "No row
selected!")
return

record_id = table.item(selected_row, 0).text()

conn = sqlite3.connect("student_data.db")
cursor = conn.cursor()
cursor.execute("DELETE FROM student_data WHERE id=?",
(record_id,))
conn.commit()
conn.close()

refresh_table()

# Edit selected record


def edit_selected():
selected_row = table.currentRow()
if selected_row < 0:
QMessageBox.warning(window, "Selection Error", "No row
selected!")
return

record_id = table.item(selected_row, 0).text()

# Retrieve the record to edit


conn = sqlite3.connect("student_data.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM student_data WHERE id=?",
(record_id,))
row = cursor.fetchone()
conn.close()

if row:
name_input.setText(row[1])
age_input.setText(str(row[2]))
address_input.setText(row[3])
contact_number_input.setText(row[4])
email_input.setText(row[5])
gender_input.setCurrentText(row[6])
dob_input.setDate(QDate.fromString(row[7], 'yyyy-MM-dd'))
course_input.setText(row[8])
year_input.setText(str(row[9]))
feedback_input.setPlainText(row[10])

# PyQt5 window setup


app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Student Data Form")

# Set window style and size


window.setStyleSheet("""
QWidget {
font-size: 14px;
font-family: Arial;
background-color: #f4f4f9;
}
QPushButton {
background-color: #4CAF50;
color: white;
font-size: 16px;
border-radius: 5px;
padding: 10px 20px;
margin-top: 10px;
}
QPushButton:hover {
background-color: #45a049;
}
QTableWidget {
font-size: 14px;
font-family: Arial;
background-color: white;
selection-background-color: #00aaff;
}
QLineEdit, QTextEdit, QSpinBox, QComboBox, QDateEdit {
padding: 10px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 5px;
}
QComboBox {
background-color: #fff;
}
QSpinBox {
background-color: #fff;
}
QDateEdit {
background-color: #fff;
}
""")

# Layout setup
layout = QVBoxLayout(window)

# Form Layout
form_layout = QFormLayout()
name_input = QLineEdit()
age_input = QLineEdit()
address_input = QLineEdit()
contact_number_input = QLineEdit()
email_input = QLineEdit()
gender_input = QComboBox()
gender_input.addItems(["Male", "Female", "Other"])
dob_input = QDateEdit()
dob_input.setCalendarPopup(True)
course_input = QLineEdit()
year_input = QLineEdit()
feedback_input = QTextEdit()

form_layout.addRow("Name:", name_input)
form_layout.addRow("Age:", age_input)
form_layout.addRow("Address:", address_input)
form_layout.addRow("Contact Number:", contact_number_input)
form_layout.addRow("Email:", email_input)
form_layout.addRow("Gender:", gender_input)
form_layout.addRow("Date of Birth:", dob_input)
form_layout.addRow("Course:", course_input)
form_layout.addRow("Year:", year_input)
form_layout.addRow("Feedback/Suggestions:", feedback_input)

# Buttons Layout
buttons_layout = QHBoxLayout()
submit_button = QPushButton("Submit")
clear_button = QPushButton("Clear")
delete_button = QPushButton("Delete Selected")
edit_button = QPushButton("Edit Selected")

submit_button.clicked.connect(submit_form)
clear_button.clicked.connect(clear_form)
delete_button.clicked.connect(delete_selected)
edit_button.clicked.connect(edit_selected)

buttons_layout.addWidget(submit_button)
buttons_layout.addWidget(clear_button)
buttons_layout.addWidget(delete_button)
buttons_layout.addWidget(edit_button)

# Table Layout
table = QTableWidget()
table.setColumnCount(11)
table.setHorizontalHeaderLabels(
["ID", "Name", "Age", "Address", "Contact Number", "Email", "Gender",
"DOB", "Course", "Year", "Feedback"]
)
table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)

# Add widgets to layout


layout.addLayout(form_layout)
layout.addLayout(buttons_layout)
layout.addWidget(table)

# Initialize database
initialize_db()

# Show the window


window.resize(800, 600)
window.show()

# Run the application


refresh_table()
sys.exit(app.exec_())
Database::

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy