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

CS 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CS 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

AIM

Write a program to display all the records along with


the total number of records from student table using
python shell.

DOCUMENTATION
• NAME OF THE SOURCE FILE: 4.26 PI-19
• SIZE OF THE FILE: 3.00KB
• VERSION OF PYTHON USED: Python 3.11.4
• MODULE OF PYTHON USED: IDLE
• FUNCTIONS IN PYTHON USED: sqlite3.connect()
conn.cursor(),cursor.execute(),cursor.fetchall(),conn.co
mmit(),conn.close(),len()
Algorithm

Part 1: Database Creation and Data Insertion


1. Start
2. Import SQLite Library:
 Import the sqlite3 module.
3. Establish Database Connection:
 Connect to the SQLite database
(e.g., school.db).
4. Create a Cursor Object:
 Use the connection object to create a cursor.
5. Create Student Table:
 Execute SQL command to create a table
named student with columns id, name, age,
and grade.
6. Insert Records:
 Execute SQL commands to insert multiple
student records into the student table:
 Insert record for Alice (1, 'Alice', 20, 'A')
 Insert record for Bob (2, 'Bob', 21, 'B')
 Insert record for Charlie (3, 'Charlie', 22,
'C')
7. Commit Changes:
 Commit the transaction to save changes to the
database.
8. Close Database Connection:
 Close the database connection.
9. End Part 1

Part 2: Data Retrieval and Display


1. Start Part 2
2. Establish Database Connection:
 Connect to the SQLite database
(e.g., school.db).
3. Create a Cursor Object:
 Use the connection object to create a cursor.
4. Fetch All Records:
 Execute SQL command to select all records
from the student table.
5. Retrieve Records:
 Use the cursor to fetch all records into a
variable (e.g., records).
6. Display Records:
 Print a header "Student Records:".
 For each record in records, print the record.
7. Count Total Records:
 Use the len() function to count the number of
records and store it in a variable
(e.g., total_records).
8. Display Total Records:
 Print the total number of records.
9. Close Database Connection:
 Close the database connection.
CODE
Create a sample SQLite database and student table

import sqlite3
# Connect to SQLite database (or create it if it doesn't
exist)
conn = sqlite3.connect('school.db')
# Create a cursor object
cursor = conn.cursor()
# Create a student table
cursor.execute('''
CREATE TABLE IF NOT EXISTS student (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER NOT NULL,
grade TEXT NOT NULL)''')
# Insert sample data into the student table
cursor.execute("INSERT INTO student (name, age,
grade) VALUES ('Alice', 20, 'A')")
cursor.execute("INSERT INTO student (name, age,
grade) VALUES ('Bob', 21, 'B')")
cursor.execute("INSERT INTO student (name, age,
grade) VALUES ('Charlie', 22, 'C')")
# Commit changes and close the connection
conn.commit()
conn.close()

Display all records along with the total number of


records:

import sqlite3
# Connect to the SQLite database
conn = sqlite3.connect('school.db')
# Create a cursor object
cursor = conn.cursor()
# Fetch all records from the student table
cursor.execute("SELECT * FROM student")
records = cursor.fetchall()
# Display all records
print("Student Records:")
for record in records:
print(record)
# Display the total number of records
total_records = len(records)
print(f"\nTotal number of records: {total_records}")
# Close the connection
conn.close()

OUTPUT

Student Records:
(1, 'Alice', 20, 'A')
(2, 'Bob', 21, 'B')
(3, 'Charlie', 22, 'C')

Total number of records: 3

You might also like