0% found this document useful (0 votes)
5 views3 pages

SQLite Integration for Student Management

This document outlines the integration of SQLite database connectivity into a Python-based Student Management System, replacing the previous file-handling system. It details the benefits of using SQLite, the database table structure, and provides code snippets for adding, displaying, searching, modifying, and deleting student records. Additionally, it includes references to relevant documentation for further reading.

Uploaded by

sototor602
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)
5 views3 pages

SQLite Integration for Student Management

This document outlines the integration of SQLite database connectivity into a Python-based Student Management System, replacing the previous file-handling system. It details the benefits of using SQLite, the database table structure, and provides code snippets for adding, displaying, searching, modifying, and deleting student records. Additionally, it includes references to relevant documentation for further reading.

Uploaded by

sototor602
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

Student Management System – SQL Connectivity

(SQLite)

This document explains how SQL database connectivity is added to the existing Python-based
Student Management System project. The file-handling system ([Link]) is replaced with an
SQLite database using Python’s official sqlite3 module.

Why SQLite?
• SQLite is built into Python and requires no server setup.
• It is recommended for beginners and academic projects.
• It provides better data integrity and scalability than text files.

Database Table Structure


CREATE TABLE students (
roll TEXT PRIMARY KEY,
name TEXT NOT NULL,
class TEXT NOT NULL,
marks REAL NOT NULL
);

Python Database Connection Code


import sqlite3

def get_connection():
return [Link]("[Link]")

Add Student Record


def add_student():
con = get_connection()
cur = [Link]()

roll = input("Enter roll number: ")


name = input("Enter name: ")
cls = input("Enter class-section: ")
marks = float(input("Enter marks: "))

try:
[Link](
"INSERT INTO students VALUES (?, ?, ?, ?)",
(roll, name, cls, marks)
)
[Link]()
print("Student added successfully.")
except [Link]:
print("Roll number already exists.")

[Link]()

Display All Students


def display_students():
con = get_connection()
cur = [Link]()
[Link]("SELECT * FROM students")
records = [Link]()

print("Roll\tName\tClass\tMarks")
for r in records:
print(r[0], r[1], r[2], r[3])

[Link]()

Search Student by Roll Number


def search_student():
roll = input("Enter roll number to search: ")

con = get_connection()
cur = [Link]()

[Link]("SELECT * FROM students WHERE roll = ?", (roll,))


record = [Link]()

if record:
print("Record found:", record)
else:
print("Record not found.")

[Link]()

Modify Student Record


def modify_student():
roll = input("Enter roll number to modify: ")
con = get_connection()
cur = [Link]()

name = input("Enter new name: ")


cls = input("Enter new class-section: ")
marks = float(input("Enter new marks: "))

[Link](
"UPDATE students SET name=?, class=?, marks=? WHERE roll=?",
(name, cls, marks, roll)
)

if [Link] == 0:
print("Record not found.")
else:
print("Record modified.")

[Link]()
[Link]()

Delete Student Record


def delete_student():
roll = input("Enter roll number to delete: ")

con = get_connection()
cur = [Link]()

[Link]("DELETE FROM students WHERE roll=?", (roll,))

if [Link] == 0:
print("Record not found.")
else:
print("Record deleted.")

[Link]()
[Link]()
References
Python sqlite3 Documentation: [Link]
SQLite Official Documentation: [Link]
CBSE Computer Science Curriculum (083): [Link]

Common questions

Powered by AI

Parameterized queries are significant in the SQLite operations of the Student Management System because they help prevent SQL injection attacks. By using placeholders such as '?' in the SQL commands, the system safely interpolates user input into queries, separating SQL logic from data. This practice not only enhances security but also improves the robustness of the code by minimizing the risk of syntactic errors due to improperly formatted queries. As a result, the system maintains both security and reliability in its database operations .

Closing the database connection after completing operations is crucial because it ensures that resources are released back to the system, preventing memory leaks and potential locking issues. In the context of the Student Management System, this practice not only optimizes resource use by maintaining system stability but also ensures that data is properly committed and saved if transactions have been performed. It protects against data corruption and promotes efficient management of database access .

The Python sqlite3 module contributes significantly to the functionality of the Student Management System by providing the means to directly interact with the SQLite database. It allows the implementation of CRUD operations (Create, Read, Update, Delete) through executing SQL commands in a Pythonic way. The module ensures database connections are easily managed and provides built-in methods for error handling and transaction management, which are crucial for maintaining data consistency and reliability within the system .

Changing the 'marks' field type from REAL to INTEGER would impact the system by affecting the granularity and precision of the data stored. Marks that include decimal points (e.g., 85.5) would need to be rounded or truncated to fit an integer type, potentially leading to data loss or misrepresentation of students' scores. This change would necessitate an adjustment in any logic or calculations that depend on precision, and it could alter how reporting or grading based on marks is handled within the system, thus requiring thoughtful consideration of whether the change aligns with the project's requirements .

A developer might choose SQLite for an academic project because it is a lightweight, self-contained database that does not require server setup, making it easy to integrate and manage within the Python environment. SQLite's simplicity and ease of use make it particularly well-suited to educational settings and projects of a smaller scale where advanced features of a server-based SQL database are not required. Additionally, SQLite offers better data integrity than file handling solutions, which is an important consideration for maintaining consistent data management .

The system handles attempts to delete a non-existing student by executing a DELETE SQL command and then checking the rowcount attribute of the cursor object. If the rowcount is zero, it indicates that no records were matched and therefore none were deleted. The system responds to this by displaying a 'Record not found' message, informing the user that the operation did not affect any data, thus ensuring proper error handling and user feedback .

To modify an existing student record, the user is prompted to enter the roll number of the student to be modified. The system then establishes a database connection, and using a cursor, executes an UPDATE SQL command with the new details for name, class, and marks. The execution is dependent on a matching roll number, which ensures the correct record is updated. The rowcount attribute is checked post-execution to confirm whether any record was modified. If no records are found (rowcount is zero), a message is displayed indicating that the record was not found, ensuring user feedback on the operation's success .

SQLite offers several advantages over traditional text file handling in a Python-based Student Management System, including improved data integrity and scalability. Unlike text files, SQLite provides transactional capabilities and maintains consistent data even during failures or crashes. It also allows for complex queries and indexing, facilitating efficient data retrieval and manipulation. Additionally, SQLite is built into Python and requires no server setup, making it ideal for beginners and academic projects .

To add a new student to the SQLite database, a connection to the database is established using sqlite3.connect, and a cursor is created. The student's details are collected through input prompts, and an INSERT SQL command is executed with these details. Error handling is implemented using a try-except block, specifically catching sqlite3.IntegrityError to handle situations where a duplicate roll number is attempted to be added, ensuring data integrity by preventing primary key violations .

The 'PRIMARY KEY' constraint in the 'students' table serves to uniquely identify each record by ensuring that the roll number is unique across the database. This constraint prevents duplicate entries with the same roll number, maintaining the integrity and reliability of the data. It affects database operations by enforcing uniqueness during data entry and modifications, and any attempts to enter a record with a duplicate roll number will raise an sqlite3.IntegrityError, thereby preventing the operation .

You might also like