Python 3.7: How To Iterate Through A Tuple In Python

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Thanks man. Another little exercise along the way. Appreciate it.

barnabykent
Автор

Hi, Hope you can answer this issue I am having with a tuple. I first make a connection to a remote MYSQL DB. using a SELECT STATEMENT. I then perform a fetchall() pyton function on the returned data from the table . example select: selected_data = cur.execute("SELECT * FROM user") which works fine. I then perform a fetchall() on the returned data by all_data = fetchall() which returns as a tuple type with all the records from the user table... Now I want to compare the GUI entry box text of login_name and login_pwd to that of specific elements of the tuple, all_data. (see below on the for loop I used... AND THE ERROR I GET AT RUNTIME WHEN I HIT THE LOGIN BUTTON ON THE GUI..

NOTE: THE TUPLE BEING EVALUATED IS NESTED. A TUPLE WITH EACH RECORD BEING ITS OWN TUPLE.
NOTE-2: I EVEN TRIED THIS IN PYTHON 2.7 AND IT WORKS BUT NOT ON MY MAC WITH PYTHON 3.7.4

i = 0
for item in all_data:
data_dict['uid'] = item[i][0]
data_dict['user_name'] = item[i][1]
data_dict['user_pwd'] = item[i][2]
data_dict['f_name'] = itemp[i][3]
data_dict['l_name'] = item[i][4]
data_dict['email'] = item[i][5]
data_dict['phone'] = item[i][6]
data_dict['create_dt'] = item[i][7]
print("i = " + str(i))
i = i + 1



My goal is to build a dictionary object called data_dict as the FOR LOOP iterates through the tuplw.
but this is what I get as the error: see below


ERROR I GET ON THE CONSOLE OF THE VISUAL STUDIO CODE IDE EDITOR:
TypeError: 'int' object is not subscriptable



WHAT AM I DOING WRONG. GRANTED I AM ONLY TWO WEEKS INTO LEARNING PYTHON, BUT HAVE BEEN PROGRAMING SINCE 1984 IN C, VB, ALC, COBOL, HTML 5 ETC AND NEVER RAN INTO THIS KIND OF PROBELM. Any help is appreciated with example code on how to do it right too, would be helpful... Thanks. Tim

FRIENDSofCAP
welcome to shbcf.ru