在Python中,从SQL表和列名中获取值

2024-04-25 23:23:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类似下面的SQL表

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

我使用了cursor.execute("select top 5 Key, Value from TestTable"),然后尝试使用mylist = list(cursor)将其转换为列表

但我越来越喜欢

^{pr2}$

但我需要这个表作为python中的列表,如下所示

[{'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'}]

我怎么做?在


Tags: keyvaluerebeccaedwardmichaelwilliamsbasketballfootball

热门问题