import sqlite3

database = 'instance/sqlite.db' # this is location of database

def schema():
    
    # Connect to the database file
    conn = sqlite3.connect(database)

    # Create a cursor object to execute SQL queries
    cursor = conn.cursor()
    
    # Fetch results of Schema
    results = cursor.execute("PRAGMA table_info('drivers2')").fetchall()

    # Print the results
    for row in results:
        print(row)

    # Close the database connection
    conn.close()
    
schema()

Reading drivers table in Sqlite.db

import sqlite3

def read():
    # Connect to the database file
    conn = sqlite3.connect(database)

    # Create a cursor object to execute SQL queries
    cursor = conn.cursor()
    
    # Execute a SELECT statement to retrieve data from a table
    results = cursor.execute('SELECT * FROM drivers2').fetchall()

    # Print the results
    if len(results) == 0:
        print("Table is empty")
    else:
        for row in results:
            print(row)

    # Close the cursor and connection objects
    cursor.close()
    conn.close()
    
read()
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
/Users/lukavandenboomen/vscode/Project-1/_notebooks/2023-03-16-own-database.ipynb Cell 4 in <cell line: 24>()
     <a href='vscode-notebook-cell:/Users/lukavandenboomen/vscode/Project-1/_notebooks/2023-03-16-own-database.ipynb#W5sZmlsZQ%3D%3D?line=20'>21</a>     cursor.close()
     <a href='vscode-notebook-cell:/Users/lukavandenboomen/vscode/Project-1/_notebooks/2023-03-16-own-database.ipynb#W5sZmlsZQ%3D%3D?line=21'>22</a>     conn.close()
---> <a href='vscode-notebook-cell:/Users/lukavandenboomen/vscode/Project-1/_notebooks/2023-03-16-own-database.ipynb#W5sZmlsZQ%3D%3D?line=23'>24</a> read()

/Users/lukavandenboomen/vscode/Project-1/_notebooks/2023-03-16-own-database.ipynb Cell 4 in read()
      <a href='vscode-notebook-cell:/Users/lukavandenboomen/vscode/Project-1/_notebooks/2023-03-16-own-database.ipynb#W5sZmlsZQ%3D%3D?line=7'>8</a> cursor = conn.cursor()
     <a href='vscode-notebook-cell:/Users/lukavandenboomen/vscode/Project-1/_notebooks/2023-03-16-own-database.ipynb#W5sZmlsZQ%3D%3D?line=9'>10</a> # Execute a SELECT statement to retrieve data from a table
---> <a href='vscode-notebook-cell:/Users/lukavandenboomen/vscode/Project-1/_notebooks/2023-03-16-own-database.ipynb#W5sZmlsZQ%3D%3D?line=10'>11</a> results = cursor.execute('SELECT * FROM drivers2').fetchall()
     <a href='vscode-notebook-cell:/Users/lukavandenboomen/vscode/Project-1/_notebooks/2023-03-16-own-database.ipynb#W5sZmlsZQ%3D%3D?line=12'>13</a> # Print the results
     <a href='vscode-notebook-cell:/Users/lukavandenboomen/vscode/Project-1/_notebooks/2023-03-16-own-database.ipynb#W5sZmlsZQ%3D%3D?line=13'>14</a> if len(results) == 0:

OperationalError: no such table: drivers2

Create a new driver in table in Sqlite.db

import sqlite3

def create():
    lastname = input("Enter the driver last name:")
    firstname = input("Enter the driver first name:")
    password = input("Enter the drivers password")
    points = input("Enter the drivers points")
    
    # Connect to the database file
    conn = sqlite3.connect(database)

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

    try:
        # Execute an SQL command to insert data into a table
        cursor.execute("INSERT INTO drivers2 (_lastname, _firstname, _password, _points) VALUES (?, ?, ?, ?)", (lastname, firstname, password, points))
        
        # Commit the changes to the database
        conn.commit()
        print(f"A new driver record {firstname} has been created")
                
    except sqlite3.Error as error:
        print("Error while executing the INSERT:", error)


    # Close the cursor and connection objects
    cursor.close()
    conn.close()
    
#create()

Updating a driver in table in Sqlite.db

import sqlite3

def update():
    lastname = input("Enter the updated last name")
    password = input("Enter updated password")
    if len(password) < 2:
        message = "hacked"
        password = 'gothackednewpassword123'
    else:
        message = "successfully updated"

    # Connect to the database file
    conn = sqlite3.connect(database)

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

    try:
        # Execute an SQL command to update data in a table
        cursor.execute("UPDATE drivers2 SET _password = ? WHERE _firstname = ?", (password, firstname))
        if cursor.rowcount == 0:
            # The firstname was not found in the table
            print(f"No firstname {firstname} was not found in the table")
        else:
            print(f"The row with driver id {firstname} the password has been {message}")
            conn.commit()
    except sqlite3.Error as error:
        print("Error while executing the UPDATE:", error)
        
    
    # Close the cursor and connection objects
    cursor.close()
    conn.close()
    
#update()

Deleting a driver in table in Sqlite.db

import sqlite3

def delete():
    firstname = input("Enter driver id to delete")

    # Connect to the database file
    conn = sqlite3.connect(database)

    # Create a cursor object to execute SQL commands
    cursor = conn.cursor()
    
    try:
        cursor.execute("DELETE FROM drivers2 WHERE _firstname = ?", (firstname,))
        if cursor.rowcount == 0:
            # The firstname was not found in the table
            print(f"No firstname {firstname} was not found in the table")
        else:
            # The firstname was found in the table and the row was deleted
            print(f"The row with firstname {firstname} was successfully deleted")
        conn.commit()
    except sqlite3.Error as error:
        print("Error while executing the DELETE:", error)
        
    # Close the cursor and connection objects
    cursor.close()
    conn.close()
    
#delete()

CRUD a driver in table in Sqlite.db

def menu():
    operation = input("Enter: (C)reate (R)ead (U)pdate or (D)elete or (S)chema")
    if operation.lower() == 'c':
        create()
    elif operation.lower() == 'r':
        read()
    elif operation.lower() == 'u':
        update()
    elif operation.lower() == 'd':
        delete()
    elif operation.lower() == 's':
        schema()
    elif len(operation)==0: # Escape Key
        return
    else:
        print("Please enter c, r, u, or d") 
    menu() # recursion, repeat menu
        
try:
    menu() # start menu
except:
    print("Perform Jupyter 'Run All' prior to starting menu")
A new user record 4321 has been created
(1, 'Luka', 'LVDB', 'password', '2005-09-29')
(2, 'Bob', '4321', '64274bjr', '2000-01-01')