Dean Marko
Dean Marko

Reputation: 9

Creating a List in Python From SQL List

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.

Upvotes: 0

Views: 277

Answers (2)

Loop
Loop

Reputation: 78

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()]

Upvotes: 1

balderman
balderman

Reputation: 23815

just by changing

models.append(str(model)) --> models.append(model[0])

Upvotes: 1

Related Questions