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
  • Source Code
  • Query 1
  • Query 2
  • Query 3

Activity 2.23

Source Code

import sqlite3

def main():
    # 1. Connect to (or create) the SQLite database
    conn = sqlite3.connect("activity223.db")

    # 2. Create a cursor object to execute SQL commands
    cursor = conn.cursor()

    # 3. Create the "employees" table if it does not exist
    create_table_query = """
    CREATE TABLE IF NOT EXISTS employees (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        first_name TEXT NOT NULL,
        last_name TEXT NOT NULL,
        department TEXT NOT NULL,
        salary REAL NOT NULL
    );
    """
    cursor.execute(create_table_query)

    # 4. Prepare the data to insert
    employees_data = [
        ('Alice', 'Johnson', 'Engineering', 75000),
        ('Bob', 'Smith', 'Marketing', 55000),
        ('Charlie', 'Brown', 'Engineering', 68000),
        ('Diana', 'Adams', 'HR', 52000),
        ('Edward', 'Green', 'Marketing', 60000),
        ('Fiona', 'Walker', 'Engineering', 80000),
        ('George', 'Harris', 'Engineering', 70000),
        ('Hannah', 'Clark', 'Finance', 65000),
        ('Ian', 'Lee', 'HR', 50000),
        ('Jasmine', 'Wong', 'Finance', 77000)
    ]

    # 5. Insert the data into the "employees" table
    insert_query = """
    INSERT INTO employees (first_name, last_name, department, salary)
    VALUES (?, ?, ?, ?);
    """

    # Use 'executemany' to insert multiple records at once
    cursor.executemany(insert_query, employees_data)

    # 6. Commit the transaction to persist changes
    conn.commit()

    # 7. (Optional) Retrieve and print the inserted rows to confirm
    select_query = "SELECT * FROM employees;"
    cursor.execute(select_query)
    rows = cursor.fetchall()

    print("Inserted rows in the 'employees' table:")
    for row in rows:
        print(row)

    # 8. Close the connection
    conn.close()

def main2():
    # 1. Connect to (or create) the SQLite database
    #    The database file will be named "school.db".
    conn = sqlite3.connect("activity233.db")

    # 2. Create a cursor object to execute SQL commands
    cursor = conn.cursor()

    # 3. Create the "students" table if it does not exist
    #    We include columns that are typical for high school student data.
    create_table_query = """
    CREATE TABLE IF NOT EXISTS students (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        first_name TEXT NOT NULL,
        last_name TEXT NOT NULL,
        grade_level INTEGER NOT NULL,
        gpa REAL NOT NULL,
        favorite_subject TEXT
    );
    """
    cursor.execute(create_table_query)

    # 4. Prepare a list of 10 sample records for insertion
    #    These rows reflect a mix of grade levels, GPAs, and subjects.
    students_data = [
        ("John", "Smith", 9, 3.2, "Math"),
        ("Emily", "Davis", 11, 3.9, "English"),
        ("Michael", "Johnson", 10, 2.8, "History"),
        ("Sarah", "Lee", 12, 3.7, "Biology"),
        ("Thomas", "Garcia", 9, 3.5, "Physical Education"),
        ("Olivia", "Miller", 12, 4.0, "Chemistry"),
        ("Daniel", "Wilson", 11, 2.9, "Geography"),
        ("Sophia", "Brown", 10, 3.6, "English"),
        ("Liam", "Taylor", 9, 3.0, "Art"),
        ("Ava", "Martinez", 10, 3.4, "Music")
    ]

    # 5. Insert the data into the "students" table
    insert_query = """
    INSERT INTO students (first_name, last_name, grade_level, gpa, favorite_subject)
    VALUES (?, ?, ?, ?, ?);
    """

    # Use 'executemany' to insert multiple records at once
    cursor.executemany(insert_query, students_data)

    # 6. Commit the transaction to save changes
    conn.commit()

    # 7. (Optional) Retrieve and print the inserted rows to confirm
    cursor.execute("SELECT * FROM students;")
    rows = cursor.fetchall()

    print("Inserted rows in the 'students' table:")
    for row in rows:
        print(row)

    # 8. Close the connection
    conn.close()

if __name__ == "__main__":
    main()
    main2()

Query 1

SELECT * FROM employees LIMIT 2;

Query 2

SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;

Query 3

SELECT first_name, last_name, salary
FROM employees
WHERE department = 'HR'
ORDER BY salary ASC
LIMIT 2;

PreviousActivity 2.22NextProject 5

Last updated 3 months ago