2

I have a SQL table like below

----------------------------------
     Key         |    Value
----------------------------------
Ralph Williams      Football
Michael Tippett     Basketball
Edward Elgar        Baseball
Rebecca Clarke      Netball
Ethel Smyth         Badminton

I have used cursor.execute("select top 5 Key, Value from TestTable") and then tried to convert as list using mylist = list(cursor)

But I am getting like

[(u'Ralph Williams', u'Football'), (u'Michael Tippett', u'Basketball'), (u'Edward Elgar', u'Baseball'), (u'Rebecca Clarke', u'Netball'), (u'Ethel Smyth', u'Rugby')]

But I need this table as list in python like below

[{'Key':'Ralph Williams', 'Value':'Football'}, {'Key':'Michael Tippett', 'Value':'Basketball'}, {'Key':'Edward Elgar', 'Value':'Baseball'}, {'Key':'Rebecca Clarke', 'Value':'Netball'}, {'Key':'Ethel Smyth', 'Value':'Rugby'}]

How can I make that?

2 Answers 2

4

You need to use a dict cursor - see the pymssql docs.

cursor = conn.cursor(as_dict=True)
cursor.execute(...)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

Key_value_pair = dict(list_of_tuple_from_the_database)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.