CS 1
CS 1
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
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()
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')