Intro to Coding
  • Unit 1
    • Introduction
  • Activity 1.1
  • Activity 1.2
  • Activity 1.3
  • Activity 1.4
  • Activity 1.5
  • Activity 1.6
  • Activity 1.7
  • Activity 1.8
  • Activity 1.9
  • Activity 1.10
  • Activity 1.11
  • Activity 1.12
  • Activity 1.13
  • Project 1
  • Activity 1.14
  • Activity 1.15
  • Activity 1.16
  • Activity 1.17
  • Activity 1.18
  • Activity 1.19
  • Activity 1.20
  • Activity 1.21
  • Activity 1.22
  • Activity 1.23
  • Activity 1.24
  • Activity 1.25
  • Activity 1.26
  • Project 2
  • Activity 1.27
  • Activity 1.28
  • Activity 1.29
  • Activity 1.30
  • Unit 2
    • Introduction
  • Activity 2.1
  • Activity 2.2
  • Activity 2.3
  • Project 3
  • Activity 2.4
  • Activity 2.5
  • Activity 2.6
  • Activity 2.7
  • Project 4
  • Activity 2.8
  • Activity 2.9
  • Activity 2.10
  • Activity 2.11
  • Activity 2.12
  • Activity 2.13
  • Activity 2.14
  • Activity 2.15
  • Activity 2.16
  • Activity 2.17
  • Activity 2.18
  • Activity 2.19
  • Activity 2.20
  • Activity 2.21
  • Activity 2.22
  • Activity 2.23
  • Project 5
  • Activity 2.24
  • Activity 2.25
  • Activity 2.26
  • Unit 3
    • Introduction
  • Activity 3.1
  • Activity 3.2
  • Activity 3.3
  • Activity 3.4
  • Activity 3.5
  • Activity 3.6
  • Activity 3.7
  • Activity 3.8
  • Activity 3.9
  • Activity 3.10
  • Activity 3.11
  • Activity 3.12
  • Project 6
  • Activity 3.13
  • Activity 3.14
  • Activity 3.15
  • Activity 3.16
  • Activity 3.17
  • Activity 3.18
  • Activity 3.19
  • Activity 3.20
  • Activity 3.21
  • Activity 3.22
  • Activity 3.23
  • Project 7
  • Activity 3.24
Powered by GitBook
On this page

Activity 2.14

Source Code

import sqlite3
import os

def main():
    done = False
    # Connect to the SQLite database
    db = 'activity214.db'
    conn = sqlite3.connect(db)
    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 - DELETE Example")
        print("E5 - DROP DB Example")
        print("Q - Quit")
        choice = input("Choice: ")
        match choice:
            case "E1":
                create(conn)
            case "E2":
                insert(conn)
            case "E3":
                select(conn)
            case "E4":
                delete(conn)
            case "E5":
                drop(db)
            case "Q":
                print("Quitting!")
                done = True
            # default case
            case _:
                print("Invalid, try again!")

def create(conn):
    print("Create Table")

    # Create a cursor object using the cursor() method
    cursor = conn.cursor()

    # Create table
    sql = '''CREATE TABLE IF NOT EXISTS students (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL,
            age INTEGER
            )'''

    # Execute query
    cursor.execute(sql)
    print("SQL query executed")

def insert(conn):
    print("Insert Data")

    # Create a cursor object using the cursor() method
    cursor = conn.cursor()

    # Insert data into table
    cursor.execute("INSERT INTO students (name, age) VALUES \
                ('Alice', 21), \
                ('Bob', 22), \
                ('Charlie', 20)")

    # 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
    cursor.execute("SELECT * FROM students")

    # fetch the rows
    rows = cursor.fetchall()
    for row in rows:
        print(row)
    print("SQL query executed")

def delete(conn):
    print("Delete")
    cursor = conn.cursor()
    cursor.execute("DELETE FROM students")
    conn.commit()
    print("changed committed")

def drop(db):
    print("dropping database file: " + str(db))
    # Remove the database file
    os.remove(db)
    print("database deleted")

main()
PreviousActivity 2.13NextActivity 2.15

Last updated 4 months ago