Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting Table and Column names in PyOdbc
    primarykey
    data
    text
    <p>I'd like to retrieve the fully referenced column name from a PyOdbc Cursor. For example, say I have 2 simple tables:</p> <ul> <li><code>Table_1(Id, &lt; some other fields &gt;)</code></li> <li><code>Table_2(Id, &lt; some other fields &gt;)</code></li> </ul> <p>and I want to retrieve the joined data</p> <pre><code>select * from Table_1 t1, Table2 t2 where t1.Id = t2.Id </code></pre> <p>using pyodbc, like this:</p> <pre><code>query = 'select * from Table_1 t1, Table2 t2 where t1.Id = t2.Id' import pyodbc conn_string = '&lt;removed&gt;' connection = pyodbc.connect(conn_string) cursor = connection.cursor()cursor.execute(query) </code></pre> <p>I then want to get the column names:</p> <pre><code>for row in cursor.description: print row[0] </code></pre> <p>BUT if I do this I'll get <code>Id</code> twice which I don't want. Ideally I could get <code>t1.Id</code> and <code>t2.Id</code> in the output.</p> <p>Some of the solutions I've thought of (and why I don't really want to implement them):</p> <ol> <li>re-name the columns in the query - in my real-world use case there are dozens of tables, some with dozens of rows that are changed far too often</li> <li>parse my query and automate my SQL query generation (basically checking the query for tables, using the cursor.tables function to get the columns and then replacing the <code>select *</code> with a set of named columns) - If I have too I'll do this, but it seems like overkill for a testing harness</li> </ol> <p>Is there a better way? Any advice would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload