3
mycursor.execute("SELECT Name FROM employee_details")
names =  (mycursor.fetchall())
for y in names:
    print (y)

Result:

('dsf',)
('Gai',)
('egw',)
('qwr',)
('Sam',)

Result I want:

dsf
Gai
egw
qwr
Sam
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828

3 Answers3

3

cursor.fetchall() return list of tuples. you can access single values from tuple using index.

for y in names:
    print (y[0])
deadshot
  • 8,881
  • 4
  • 20
  • 39
2

Add a comma after the loop value in the for statement, to assign a name to the element in the tuple, and omit the comma when printing, to get only the element:

for y, in names:
    print(y)

As @deadshot mentions in the comments, this works because of Python's tuple-unpacking syntax. It's no different to

a, b = (1, 2)

except that there is a single-element tuple involved, and we tend to think of tuples as having more than one element

a, = (1,)
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
-1

You can save result to string and replace ('') with " ". This is easy way to do it :D.

String = result.replace("('", "")
String = String.replace("')", " ")
Michael Štekrt
  • 406
  • 3
  • 8