Get values from SQL table along with column name as list in Python

Prabhakar

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?

Daniel Roseman

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

cursor = conn.cursor(as_dict=True)
cursor.execute(...)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get values from SQL table along with column name as list in Python

From Dev

SQL - get list of different values from column

From Dev

SQL - get list of different values from column

From Dev

How to get list of constraints of a table along with their respective column names in SQL Server 2008 R2

From Dev

Qt/SQL - Get column type and name from table without record

From Dev

Qt/SQL - Get column type and name from table without record

From Dev

Sql Query to get column name from another table

From Dev

Get both data and column name from sql table

From Dev

How to get column name list from java.sql.ResultSet

From Dev

Get distinct values from each table and each column with SQL Server

From Dev

Get column name from a table's dataset in python

From Dev

SQL query to retrieve 20 columns along with there values from the given table

From Dev

SQL table column values to select query list

From Dev

SQL table column values to select query list

From Dev

get values from table in column from database

From Dev

Get Values from DataTable by row and column name

From Dev

Excel: get list of unique values from a table and place the values in one column using formulas

From Dev

Return best values of two SQL tables along with the values from table 1 that has no match in table2

From Dev

XPath read values from sql table column

From Dev

Populate a column in SQL with values from another table

From Dev

XPath read values from sql table column

From Dev

SQL get multiple values (from the same column) from same table using multiple queries

From Dev

Get column name from SQL Server

From Dev

Get MAX from row with column name (SQL)

From Dev

Get name of column from PowerShell sql query

From Dev

Oracle - get table name from sql text

From Dev

SQL get name on the id from other table

From Dev

How to get value from one table column when two columns of the same name exist in an sql join

From Dev

get sum of column values from SQL query

Related Related

  1. 1

    Get values from SQL table along with column name as list in Python

  2. 2

    SQL - get list of different values from column

  3. 3

    SQL - get list of different values from column

  4. 4

    How to get list of constraints of a table along with their respective column names in SQL Server 2008 R2

  5. 5

    Qt/SQL - Get column type and name from table without record

  6. 6

    Qt/SQL - Get column type and name from table without record

  7. 7

    Sql Query to get column name from another table

  8. 8

    Get both data and column name from sql table

  9. 9

    How to get column name list from java.sql.ResultSet

  10. 10

    Get distinct values from each table and each column with SQL Server

  11. 11

    Get column name from a table's dataset in python

  12. 12

    SQL query to retrieve 20 columns along with there values from the given table

  13. 13

    SQL table column values to select query list

  14. 14

    SQL table column values to select query list

  15. 15

    get values from table in column from database

  16. 16

    Get Values from DataTable by row and column name

  17. 17

    Excel: get list of unique values from a table and place the values in one column using formulas

  18. 18

    Return best values of two SQL tables along with the values from table 1 that has no match in table2

  19. 19

    XPath read values from sql table column

  20. 20

    Populate a column in SQL with values from another table

  21. 21

    XPath read values from sql table column

  22. 22

    SQL get multiple values (from the same column) from same table using multiple queries

  23. 23

    Get column name from SQL Server

  24. 24

    Get MAX from row with column name (SQL)

  25. 25

    Get name of column from PowerShell sql query

  26. 26

    Oracle - get table name from sql text

  27. 27

    SQL get name on the id from other table

  28. 28

    How to get value from one table column when two columns of the same name exist in an sql join

  29. 29

    get sum of column values from SQL query

HotTag

Archive