0% found this document useful (0 votes)
1 views

Practical Task

Uploaded by

1jt21is053
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)
1 views

Practical Task

Uploaded by

1jt21is053
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/ 2

Solution for Practical Task by Srivatsa G

import sqlite3

# Connect to the SQLite database

conn = sqlite3.connect('school.db')

cursor = conn.cursor()

# Create the tables

cursor.execute('''

CREATE TABLE IF NOT EXISTS students (

student_id INTEGER PRIMARY KEY,

name TEXT NOT NULL,

age INTEGER NOT NULL,

course TEXT NOT NULL

''')

# Add a new student to the database

def add_student(student_id, name, age, course):

cursor.execute('''

INSERT INTO students (student_id, name, age, course)

VALUES (?, ?, ?, ?)

''', (student_id, name, age, course))

conn.commit()

print(f"Student {name} added successfully.")

# Retrieve names of students enrolled in the "Database Systems" course

def get_students_in_course(course_name):

cursor.execute('''

SELECT name FROM students WHERE course = ?

''', (course_name,))

students = cursor.fetchall()

print(f"Students enrolled in {course_name}:")


for student in students:

print(student[0])

# Update the age of a student with a given student_id

def update_student_age(student_id, new_age):

cursor.execute('''

UPDATE students SET age = ? WHERE student_id = ?

''', (new_age, student_id))

conn.commit()

print(f"Student's age with ID {student_id} updated successfully.")

# Example usage:

# Add new students

add_student(1, 'Alice', 20, 'Database Systems')

add_student(2, 'Bob', 22, 'Database Systems')

add_student(3, 'Charlie', 23, 'Database Systems')

# List students enrolled in "Database Systems"

get_students_in_course('Database Systems')

# Update age of student with ID 2 (Bob)

update_student_age(2, 23)

# Verify the age update

get_students_in_course('Database Systems')

# Close the connection

conn.close()

You might also like