MySQL/Python: 'NoneType' Error over dictionary iteration in for loop

121 views Asked by At

When I create fill in the dict() using the raw_input (so that it is in the form {'Key1' : ['Example', 'String'], 'Key2' : ['Random', 'String']}), I get there error 'NoneType' object is not iterable after the query loop iterates over the first index in 'c'. Can anyone explain?

#Give the user the option to pick which elements to sort by
patient_array = [1295, 1736, 1744, 2132]
input_parameters = raw_input('Select paramaters to sort by: ').split(', ')
input_list = [str(a) for a in input_parameters]
print input_list
parameter_dict = {'Melanoma Bank Number' : 'MB', 'Chromosome' : 'Chromosome', 'Start' : 'Start', 'End' : 'End', 'Gene' : 'Gene Type1'}
query_list = dict()
#Query parameters within the chosen elements
for parameter in input_list:
    parameter_input = raw_input('Select ' + parameter + ': ').split(', ')
    parameter_input = [str(a) for a in parameter_input]
    print parameter_input
    query_list[parameter] = parameter_input
print query_list    


#First query loop
def query_loop(n):
    c = query_list[input_list[n]]
    for index in patient_array:
        for x in c:
            cursor.execute("SELECT * FROM `%s` WHERE `%s` = '%s'" % (index, parameter_dict[input_list[n]], x))
            row = cursor.fetchone()
            while(row):
                #print row
                row = cursor.fetchone()
                cursor2.execute("INSERT INTO query_table VALUES (\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \'%s\')" % tuple(row))
                conn.commit()

#Subquery for parameter(n) where n >0
def subquery(n):
    c = query_list[input_list[n]]
    for x in c:
        cursor3.execute("DELETE FROM query_table WHERE `%s` <> '%s'" % (parameter_dict[input_list[n]], x))
        conn.commit()

Here is my while loop I use to iterate of the two functions:

n = 0 
while n <= len(input_list):
    if n == 0:
        query_loop(n)
        n = n + 1
    elif n < len(input_list):
        subquery(n)
        n = n + 1
    else:
        cursor.execute("SELECT * FROM query_table")
        print cursor.fetchall()
    n = n + 1    
0

There are 0 answers