0

I'm trying to do search operation, I can simply do it by

select * 
from <tablename> 
where clause

but I want to do it by traversing through list but I get an error

TypeError: list indices must be integers or slices, not tuple

Code:

 import mysql.connector as SQL
 con=sql.connect(host="localhost",user="root",passwd='root',database='kunal')
 cur=con.cursor()
 query="select * from student"
 cur.execute(query)
 data=cur.fetchall()
 search=input("RollNo: ")
 for i in data:
     list(data[i])
     if search==data[i][search]:
         print(f'Record found: {i}')            
       
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • `search` is a `str`, you need to convert it with `search = int(search)` for example. – snakecharmerb Feb 10 '22 at 17:19
  • @snakecharmerb i tried that but still its showing error ` (102, 'student2', '50', '50', '50'), (103, 'student3', '70', '71', '72'), (104, 'student4', '80', '81', '82'), (105, 'student5', '90', '91', '92'), (106, 'student6', '40', '41', '42'), (107, 'student7', '63', '64', '65')]` data record – Kunal Telangi Feb 10 '22 at 17:30
  • `i` is a tuple, one row in the resultset. `data[i]` doesn't make sense. You probably want something like `if search in i` or `if search == i[2]` if you are trying to match a particular column, – snakecharmerb Feb 10 '22 at 17:55

0 Answers0