-1

i want to use python to populate a database. I have to get values out of a table, calculate a score and fill this score into anoter table. I cant figure out how i can compare the column names of a row from a resultset. Because i dont need all of the columns to calculate, but need a few others as id and type_name for none calculation. So basically i want to do this:

cur = connection.cursor()
QUERY ="SELECT * FROM table"
cur.execute(QUERY)
rs = cur.fetchall()
for row in rs:
    for col in row:
        // if(col = "X" or col = "Y" or col = "Z"):
               calc ...
        // else:
               use id, type_name whatever ...

how can achieve something like this? Else the code would just blow up like a bomb.

  • Like this http://stackoverflow.com/questions/5010042/mysql-get-column-name-or-alias-from-query ? – Ceppo93 Jul 19 '16 at 12:51

1 Answers1

0

Maybe someone is searching for the answer too. With help of the previous comment, i could solve it like that

field_names = cur.description 
for row in rs:
    for index, col in enumerate(row):
        name = field_names[index][0]
        if(name == "..."):
        ...
        elif(name == "..."):
        ...