I’m connecting to a database using pyodbc and need to extract information from certain tables.
I’m creating a unique id for each row in a table based on the values of certain column names. I’m obtaining each column name used for the id from a config file.
I am able to do this with the column names hard coded:
connection = pyodbc.connect("connection string")
cursor = connection.cursor()
cursor.execute("select * from " + t) //t is the table name
rows = cursor.fetchall()
for row in rows:
id = `row.GroupID` + `row.Leg`
But how can I replace the column names with the names in my config file?
Assuming you have your column names already read from the config file and stored in a list (named ‘columns’), you could do something along the lines of this:
This fetches the attributes from the row objects and puts them in a list, which is then concatenated together using join, just like in your example.