Referencing fields (columns) by field name in Teradata
<p>I am using python (3.4.3) to ODBC to a Teradata database, (rather new to this) I am wondering (if possible) to reference values of rows by their field name as i am looping through them instead of by their list index. (in case i change my tables) Much like a record set in VBA with the ! syntax (recordset!FIELD_NAME)</p>

<p>If i run this code,</p>

<pre><code>udaExec = teradata.UdaExec (appName="HelloWorld", version="1.0", logConsole=False)
session = udaExec.connect(method="odbc", dsn="TEST")
cursor = session.cursor()
rows = list(cursor.execute("SELECT TOP 1 * FROM RES_TBL"))
print(rows)
</code></pre>

<p>My output is: <code>[&lt;teradata.util.Row object at 0x000000000402D080&gt;]</code></p>

<p>I eventually was able to store each row as string in a list so i could see them\ mess with them, but i feel like thats a bad idea for larger data sets. I am sorry if this is not a good question, but anything helps!!</p>

<p>my full code currently is:</p>

<pre><code>import teradata
import pyodbc
import json
udaExec = teradata.UdaExec (appName="HelloWorld", version="1.0", logConsole=False)
session = udaExec.connect(method="odbc", dsn="TEST")
cursor = session.cursor()
rows = list(cursor.execute("SELECT TOP 1 * FROM RES_TBL"))
print(rows)
for row in session.execute("SELECT TOP 1 * FROM RES_TBL"):
    testlist = []
    testlist.append(str(row))
    print(testlist)
</code></pre>

<p>Hello for anyone else that is trying to figure this out, I HAVE SOLVED IT w00t!</p>

<p>I found great links for help here, there are some programming wizzards in those links, i learned lots!!
<a href="http://stackoverflow.com/questions/3286525/return-sql-table-as-json-in-python">return SQL table as JSON in python</a></p>

<p><a href="http://stackoverflow.com/questions/16519385/output-pyodbc-cursor-results-as-python-dictionary">Output pyodbc cursor results as python dictionary</a></p>

<p><a href="http://developer.teradata.com/tools/reference/teradata-python-module" rel="nofollow">http://developer.teradata.com/tools/reference/teradata-python-module</a></p>

<p>Here is the solution!!!</p>

<pre><code>import teradata
import pyodbc
import json
###Dev environment for learning teradata interactions with python

#form the ODBC connection to teradata
try:
    udaExec = teradata.UdaExec (appName="HelloWorld", version="1.0", logConsole=False)
    session = udaExec.connect(method="odbc", dsn="TEST")
    cursor = session.cursor()
    columnNames = []
    rows = cursor.execute("SELECT TOP 1 * FROM RES_TBL")
    for col in rows.description:
        columnNames.append(col[0])

    for row in rows: #session.execute("SELECT TOP 1 * FROM RES_TBL"):
        testlist = []
        testlist.append(dict(zip(columnNames, row)))
    print(testlist)
except:
    raise
finally:
    cursor.close()
</code></pre>

python teradata