MySQL / Python:'NoneType'for循环中字典迭代的错误

时间:2015-06-15 22:10:16

标签: python mysql sql for-loop nonetype

当我使用raw_input创建填充dict()时(因此它的格式为{'Key1' : ['Example', 'String'], 'Key2' : ['Random', 'String']}),在查询循环遍历第一个索引之后,我得到错误'NoneType' object is not iterable C'。谁能解释一下?

#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()

这是我用来迭代这两个函数的while循环:

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 个答案:

没有答案