简体   繁体   中英

Can I get table names along with column names using .description() in Python's DB API?

I am using Python with SQLite 3. I have user entered SQL queries and need to format the results of those for a template language.

So, basically, I need to use .description of the DB API cursor (PEP 249), but I need to get both the column names and the table names, since the users often do joins.

The obvious answer, ie to read the table definitions, is not possible -- many of the tables have the same column names.

I also need some intelligent behaviour on the column/table names for aggregate functions like avg(field)...

The only solution I can come up with is to use an SQL parser and analyse the SELECT statement (sigh), but I haven't found any SQL parser for Python that seems really good?

I haven't found anything in the documentation or anyone else with the same problem, so I might have missed something obvious?

Edit: To be clear -- the problem is to find the result of an SQL select, where the select statement is supplied by a user in a user interface. I have no control of it. As I noted above, it doesn't help to read the table definitions.

Python's DB API only specifies column names for the cursor.description (and none of the RDBMS implementations of this API will return table names for queries...I'll show you why).

What you're asking for is very hard, and only even approachable with an SQL parser...and even then there are many situations where even the concept of which "Table" a column is from may not make much sense.

Consider these SQL statements:

  1. Which table is today from?

     SELECT DATE('now') AS today FROM TableA FULL JOIN TableB ON TableA.col1 = TableB.col1; 
  2. Which table is myConst from?

     SELECT 1 AS myConst; 
  3. Which table is myCalc from?

     SELECT a+b AS myCalc FROM (select t1.col1 AS a, t2.col2 AS b FROM table1 AS t1 LEFT OUTER JOIN table2 AS t2 on t1.col2 = t2.col2); 
  4. Which table is myCol from?

     SELECT SUM(a) as myCol FROM (SELECT a FROM table1 UNION SELECT b FROM table2); 

The above were very simple SQL statements for which you either have to make up a "table", or arbitrarily pick one... even if you had an SQL parser!

What SQL gives you is a set of data back as results. The elements in this set can not necessarily be attributed to specific database tables. You probably need to rethink your approach to this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM