Activity 3.2
Source Code
import sqlite3
import os
def main():
# Connect to the SQLite database
db = 'activity32.db'
conn = sqlite3.connect(db)
done = False
print("Welcome to the Main Function")
while not done:
print("Menu")
print("E1 - CREATE Example")
print("E2 - INSERT Example")
print("E3 - SELECT Example")
print("E4 - SELECT Example v2")
print("E5 - DELETE Example")
print("Q - Quit")
choice = input("Choice: ").upper()
match choice:
case "E1":
create(conn)
case "E2":
insert(conn)
case "E3":
select(conn)
case "E4":
select2(conn)
case "E5":
delete(conn)
case "Q":
print("Quitting!")
done = True
# default case
case _:
print("Invalid, try again!")
# close the connection
conn.close()
def create(conn):
print("Create Table")
# Create a cursor object using the cursor() method
cursor = conn.cursor()
# Create table query
sql = '''
'''
# Execute query
cursor.execute(sql)
print("Table created successfully!")
def insert(conn):
print("Insert Data")
# Create a cursor object using the cursor() method
cursor = conn.cursor()
# student data
# Insert data into table query
sql = """
"""
# Insert data into table query
print("SQL query executed")
# Commit changes
conn.commit()
print("changed committed")
def select(conn):
print("Select Data")
# Create a cursor object using the cursor() method
cursor = conn.cursor()
# SQL Query to SELECT all students
sql = "SELECT * FROM students"
# Execute query
cursor.execute(sql)
print("SQL query executed")
# fetch the rows
rows = cursor.fetchall()
for row in rows:
print(row)
def select2(conn):
print("Select Data")
# Create a cursor object using the cursor() method
cursor = conn.cursor()
# SQL Query to SELECT certain columns from students
# Execute query
cursor.execute(sql)
print("SQL query executed")
# fetch the rows
rows = cursor.fetchall()
for row in rows:
print(row)
def delete(conn):
print("Delete")
# Create a cursor object using the cursor() method
cursor = conn.cursor()
# SQL Query to DELETE all students
sql = "DELETE FROM students"
# Execute query
cursor.execute(sql)
print("SQL query executed")
# Commit Changes
conn.commit()
print("changed committed")
main()
Last updated