Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python sqlalchemy get column names dynamically?

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sqlalchemy import create_engine

connection = create_engine('mysql://user:passwd@localhost:3306/db').connect()

result = connection.execute("select * from table")
for v in result:
        print v['id']
        print v['name']
connection.close()

how i can get TABLES COLUMNS NAMES dynamically? in this case id and name

like image 731
ZiTAL Avatar asked Jan 20 '12 20:01

ZiTAL


People also ask

How do I get column names in Sqlalchemy?

To access the column names we can use the method keys() on the result. It returns a list of column names. Since, we queried only three columns, we can view the same columns on the output as well.

How do I get column values in Sqlalchemy?

Get value by column name The given task can be performed by first creating an engine with sqlalchemy, connecting with the database, and executing an SQL query with the connection. The SQL query will contain the name of the column/columns whose values we want and then we will get a result object.

How do I get column names in Python using SQL?

Connect to a database using the connect() method. Create a cursor object and use that cursor object created to execute queries in order to create a table and insert values into it. Use the description keyword of the cursor object to get the column names.

How do I select a column in Sqlalchemy?

SQLAlchemy Core The already created students table is referred which contains 4 columns, namely, first_name, last_name, course, score. But we will be only selecting a specific column. In the example, we have referred to the first_name and last_name columns. Other columns can also be provided in the entities list.


3 Answers

You can either find the columns by calling result.keys() or you can access them through calling v.keys() inside the for loop.

Here's an example using items():

for v in result:
    for column, value in v.items():
        print('{0}: {1}'.format(column, value))
like image 75
Rob Wouters Avatar answered Nov 04 '22 22:11

Rob Wouters


Most direct solution

Can't get any simpler than a one-liner solution using list comprehension. It also is the most direct method:

[col for col in result.keys()]
# return: ['id', 'name']

@Saul's answer also works, but you'll need to be careful about iterating over only the first element through each cursor.description, lest you get a bunch of None in each tuple of the returned list.

It also is less-efficient, because you need to iterate through the ResultProxy, access the cursor.description attribute and for each of them only retrieve the element at index 0.

Using timeit in Python with 500,000 iterations showed the speed difference (0.016 vs 0.011):

connection = create_engine('sqlite:///rcsample.db').connect()
result = connection.execute("select * from response")
def cursfunc():
    return [ i[0] for i in result.cursor.description ]
print(timeit.timeit("cursfunc()", setup="from __main__ import cursfunc", number=500000))
# return: 0.01606178

While the proposed solution completes in ~30% less time:

connection = create_engine('sqlite:///rcsample.db').connect()
result = connection.execute("select * from response")

def keysfunc():
    return [col for col in result.keys()]
print(timeit.timeit("keysfunc()", setup="from __main__ import cursfunc", number=500000))
# return: 0.01097001

In fact, my suspicion is that the time difference could be greater on a table with more columns than the obviously simplified example above.

In Practice: keys and values

In practice, you'd probably want to print both the key and values dynamically. There are two ways you can go about it. The first:

results = conn.execute('SELECT * FROM salesperson')
[{column:value for column, value in result.items()} for result in results]
# returns: [{'id': 1, 'name': 'John Doe'}, {'id': 2, 'name': 
# 'Margaret'}, {'id': 3, 'name': 'Anna'}]

Alternatively using unpacking:

rows = conn.execute('SELECT * FROM salesperson LIMIT 2').fetchall()
print([{**row} for row in rows])
# returns: [{'id': 1, 'name': 'John Doe'}, {'id': 2, 'name': 'Margaret'}]

Both of these methods are direct and pythonic, while also exempt the programmer from having to specify (or know beforehand) the columns names explicitly.

like image 44
onlyphantom Avatar answered Nov 04 '22 21:11

onlyphantom


something like this

headers=[ i[0] for i in result.cursor.description ]

same question here return column names from pyodbc execute() statement

like image 5
dim_user Avatar answered Nov 04 '22 20:11

dim_user