def makeModels():
make = "Acura"
models = []
for model in (cursor.execute('select model from Car where make like ' + "'{}'".format(make))):
models.append(str(model))
I get in Return:
('CL', )
('EL', )
('ILX', )
('Integra', )
('MDX', )
('NSX', )
('RDX', )
('RL', )
('RLX', )
('RSX', )
('TL', )
('TLX', )
('TSX', )
('ZDX', )
Now This is going into a ComboBox and I I cant have it look like this. I have Class With the same info So my main question is how can I change the format.. What i understand is when you pull something from SQL its and object.
asked Jul 15, 2021 at 13:51
Dean Marko
191 gold badge1 silver badge5 bronze badges
2 Answers 2
just by changing
models.append(str(model)) --> models.append(model[0])
answered Jul 15, 2021 at 14:12
balderman
24k8 gold badges39 silver badges60 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Dean Marko
this worked, why does it work if you could explain
balderman
I am glad it worked. The query return a collection of tuple. Each tuple contains one element at offset 0. I hope it is clear now.
balderman
Feel free to accept the answer if it works for you.
Try this:
def makeModels():
make = "Acura"
query = 'select model from Car where make like {}'.format(make))
cursor.execute(query)
models = [list(i)[0] for i in cursor.fetchall()]
2 Comments
Dean Marko
could you explain how I would use this to get a specific query?
Loop
I edited my answer according to your query hope this works for you. @balderman already explained how the query return and so on.
default
cursor.fetchmany()returns list of tuples, so just transform it to list of strings