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>Maybe you don't want to use pandas for some reason but otherwise I'd suggest this:</p>

<pre><code>import pandas ad pd
cursor = session.execute(SQL_script)
df = pd.DataFrame.from_records(cursor)
cols = []
for row in cursor.description:
    cols.append(row[0])
df.columns = cols
session.close()
</code></pre>

python teradata