#IMPORTS---------------------------------------------------------------

import PySimpleGUI as sg
import sqlitedb as db
from rpa_sql import *           #IN BOOK IT DOES NOT SAY IMPORT THIS BUT YOU HAVE TO
from rpa_gui_entry import show_rpa_form



#VARIABLES/LISTS (I think)-------------------------------------------------

sg.theme('Light Brown 1')
FORM_FONT = 'Calibri 16'
LABEL_SIZE = (10,1)
BUTTON_SIZE = (7,1)
BUTTON_PAD = ((10,0),(20,0))
BG = 'lightblue'

db.db_file = rpa_database           #IN BOOK IT SAYS db.db_file = mission_database BUT CHANGE IT TO db.db_file = rpa_database
rpa_list = []
rpa_data = []
data_index = -1

#WINDOW/LAYOUT-------------------------------------------------

form = [
        [ sg.Text('Registration', size=LABEL_SIZE, background_color = BG),
          sg.Text('', key = 'reg', background_color = BG)
        ],
        [ sg.Text('Type', size=LABEL_SIZE, background_color = BG),
          sg.Text('', key = 'type', background_color = BG)
        ],
]

layout = [
    [
      sg.Table(values = rpa_list, headings = ['Name','Type'],
                max_col_width=20,
                key = 'table',
                auto_size_columns = False,
                col_widths = [15,15],
                justification = 'left',
                display_row_numbers=False,
                num_rows = 10,
                background_color='white',
                alternating_row_color=('lightgray'),
                selected_row_colors=('white', 'black'),
                row_height=30,
                enable_events = True, font = FORM_FONT),
    ],

    [ sg.OK('OK', key = 'ok', pad=BUTTON_PAD, size=BUTTON_SIZE ),
      sg.Button('Add', key = 'add', pad=BUTTON_PAD, size=BUTTON_SIZE ),
      sg.Button('Edit', key = 'edit', pad=BUTTON_PAD, size=BUTTON_SIZE ),
      sg.Button('Delete', key = 'delete', pad=BUTTON_PAD, size=BUTTON_SIZE ),
    ]
]




#FUNCTIONS---------------------------------------------------------------------

def rpa_data_to_list():
    global rpa_list, data_index
    sql = """
            SELECT rpa_reg, rpa_name, rpa_weight FROM rpa
            INNER JOIN rpa_type ON rpa.rpa_type_id = rpa_type.rpa_type_id
        """
    rpa_list = db.query_table('SELECT rpa_reg FROM rpa')
    values = []
    for rpa in rpa_list:
        values.append([rpa[0]])
    window['table'].update(values = values)



def rpa_data_to_form(reg):                  #Get values for the selected rpa
    global rpa_data
    if data_index > -1:
        if reg == None: reg = rpa_list[data_index][0]
        sql_query = '''
                    SELECT * FROM rpa
                    INNER JOIN rpa_type ON rpa.rpa_type_id = rpa_type.rpa_type_id
                    WHERE rpa_reg =
                    ''' + '"'+reg+'";'
        rpa_data = db.query_table(sql_query)



def rpa_type_name_to_id(s):
    sql_query = '''
                SELECT * FROM rpa_type
                WHERE rpa_type_name =
                ''' + '"'+s+'";'            #IN BOOKLET IT SAYS rpa_name = IN THE LINE ABOVE IT NEEDS TO BE rpa_type_name =
    d = db.query_table(sql_query)
    print('rpa_id_from_name = ' ,d)
    return d[0][0]


#MISC STUFF-------------------------------------------------------------------

window = sg.Window("RPA List", layout, finalize=True, font = FORM_FONT,
                    margins = (10,20))

rpa_data_to_list()
rpa_data_to_form(None)

#MAIN LOOP--------------------------------------------------------------------

while True:
    event, values = window.read(timeout=10)
    if not event == '__TIMEOUT__': print(event,'|', values)
    if event in ["Exit", sg.WIN_CLOSED, 'ok']:
        break


    elif event == 'list':
        index = values['table'][0]
        data_index = index
        rpa_data_to_form(None)




    elif event in ['add','edit']:
        if event == 'edit' and data_index > -1:
            data = {}
            data['rpa_reg'] = rpa_data[0][1]
            data['rpa_type'] = rpa_data[0][4]
        else:
            data = None
            rpa_data = None
        data = show_rpa_form(data)

        if data != None:
            rpa = []
            rpa.append(data['rpa_reg'])
            rpa.append(rpa_type_name_to_id(data['rpa_type']))

            if rpa_data == None:        #add to the list
                sql = """
                    INSERT INTO rpa(rpa_reg, rpa_type_id)
                    VALUES(?,?);
                    """
                id = db.insert_record(sql, rpa)
                print('Added rpa record', id)

            else:                           #edit the list
                rpa.append(rpa_data[0][0])
                sql = """
                    UPDATE rpa
                    SET rpa_reg = ?, rpa_type_id = ?
                    WHERE rpa_id = ?
                    """
                id = db.update_record(sql, rpa)
                print('Updated rpa record', id)
            rpa_data_to_list()
            rpa_data_to_form(data['rpa_reg'])




    elif event == 'delete':
        if rpa_data != None:
            data = [ rpa_data[0][0] ]
            sql = """
                DELETE FROM rpa_type
                WHERE rpa_tyep_id = ?
                """
            ok = db.update_record(sql, data)
            if ok: print('Record deleted')

            rpa_data_to_list()
            rpa_data_to_form(None)






window.close()


The error:


Traceback (most recent call last):
  File "/Users/kory/mu_code/rpa_table.py", line 166, in <module>
    data = [ rpa_data[0][0] ]
IndexError: list index out of range

Help me fix my code please