1

Using SQL Alchemy, I'd like to know if it's possible to get a list of all columns from any SQL query.

For example, if I call this query:

SELECT * FROM users;

I would need all the results, but also a list of all the columns from the table users.

I know I could hit the table Informations Schema, but my problem also affect any kind of query, like :

SELECT u.*, f.name, f.size FROM users u LEFT JOIN files f ON f.user_id = u.id;

I saw this option from SQL Alchemy, [column_description][1], but I don't know if it works only for an ORM style ; I am using a non ORM style (I will write directly the queries).

2
  • 1
    General best practice in database apps in never use Select * as you know which fields you want as you have to know how to process them and then when the db is altered by adding fields it does not affect your code, also the database filed names are unlikely to be what your users call the information . So I would ask what is the user requirement you are trying to develop here. Commented Jul 22, 2012 at 20:33
  • I'm playing to build an alternative to PhpMyAdmin. I can't control what will be written in queries, and I'm pretty sure this kind of query will be written more than once. Commented Jul 23, 2012 at 6:32

1 Answer 1

2

Use ResultProxy.keys():

sql = "SELECT * FROM users;"
res_proxy = session.execute(sql)
column_names = res_proxy.keys()

However, I agree with Mark that one should not use SELECT * as a general rule.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your tip. As for SELECT *, see my comment after his ;)
Saw the comment, have no idea what PhpMyAdmin is and how it works though :). I am sure you have good reason. Good luck with the project!

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.