Let's say I have a list of tuples
users = [('Ben', 13),
('Ana', 13),
('Max', 13)]
Where ('Ben', 13) will be 0 and ('Ana', 13) will be 1 ...
EDIT
I have a table from PySimpleGUI that displays a list of tuples. Selecting a row in the table returns the row index. I want to get the 'User ID' that is tied to the returned row index so I could make use of it in a delete query.
Here's the code:
import sys
import mysql.connector
import PySimpleGUI as sg
def connect():
mydb = mysql.connector.connect(
host='localhost',
user='root',
passwd='maning',
database='usersdb'
)
return mydb
mydb = connect()
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM thesisdb.users")
data = mycursor.fetchall() #the list of tuples
headings = ['User ID', 'First Name', 'Last Name', 'Username', 'Password', 'Role']
layout = [[sg.Table(values=data, headings=headings, max_col_width=25,
auto_size_columns=True, bind_return_key=True, justification='right', num_rows=20, alternating_row_color='lightgreen', key='users')],
[sg.Button('Read'), sg.Button('Double')],
[sg.T('Read = read which rows are selected')],[sg.T('Double = double the amount of data in the table')]]
window = sg.Window('Table', grab_anywhere=False, resizable=True).Layout(layout)
while True:
event, values = window.Read()
rowIndex = values['users'] #the index of the selected row
sg.Popup(index) #displays the index of the selected row
As far as I know, the table is only capable of returning the index of the selected row. That's why I asked if it's possible to add an index number for each tuple because I want to use rowIndex
in getting the 'User ID' with the same index number of the selected row.
The delete query will hopefully be:
mycursor.execute("DELETE FROM usersdb.users WHERE user_ID = '%s'" % (userID))