Sqlite3 - 如何在变量中存储SELECT值以在连接外使用?

时间:2017-03-02 19:17:36

标签: python list for-loop sqlite zip

我正在尝试使用列表来匹配表中的每个列表项的各个值,并从另外两个表中计算每个匹配项。我认为我的代码可能会更好地解释它:

def info_check():                                                                                                                            
    conn = sqlite3.connect('database.db')                                                                                                    
    c = conn.cursor()                                                                                                                        
    for ind in zip(address, name, number):                                                                                                   
        c.execute('''SELECT * FROM table                                                                                                     
                     WHERE Column4 > Column5                                                                                                 
                       AND Column1 = ?                                                                                                       
                       AND Column2 = ?                                                                                                       
                       AND Column3 = ?''', ind)                                                                                              
        address_found = c.fetchall()                                                                                                         
        if len(address_found) >= 0:                                                                                                          
            for i, j in enumerate(zip(address, name, number)):                                                                               
                if j == ind:                                                                                                                 
                    c.execute('''SELECT * FROM table                                                                                         
                                         WHERE Column4 = Column5                                                                             
                                          AND Column1  = ?                                                                                   
                                          AND Column2 = ?                                                                                    
                                          AND Column3 = ?''', ind)                                                                           
                    name_found = c.fetchall()                                                                                                
                    if len(name_found) >= 0:                                                                                                 
                        for a, b in enumerate(zip(address, name, number)):                                                                   
                            if b == ind:                                                                                                     
                                c.execute('''SELECT * FROM table                                                                             
                                             WHERE Column4 < Column5                                                                         
                                              AND Column1 = ?                                                                                
                                              AND Column2 = ?                                                                                
                                              AND Column3 = ?''', ind)                                                                       
                                number_found = c.fetchall()                                                                                  
                                if len(number_found) >= 2 or len(address_found) >= 2 or len(name_found) >= 2:                                
                                    print(ind)   
                                    print(otherList[i])                                                                                                                      
                                    print(len(address_found), len(name_found), len(number_found))

代码工作正常,做我需要的,唯一的问题是我得到一些项目的重复,有时甚至是同一项目的4个结果。

我只是在学习python,而我正在努力寻找一种从结果中删除重复项的方法。 我想如果我可以存储每个Select值,以及它们在ind循环之外可以使用的变量中的特定ifor,也许那将删除重复?每次都要为它创建一个新变量吗?

非常感谢任何帮助;谢谢

1 个答案:

答案 0 :(得分:0)

想出来,我删除了所有不必要的条件并首先执行了所有Select变量,然后只使用了一个包含所有变量的if

def info_check():
    conn = sqlite3.connect('database.db')
    c = conn.cursor()

    for i, ind in zip(address, name, number):
        c.execute('''SELECT * FROM table
                     WHERE Column4 > Column5
                     AND Column1 = ?
                     AND Column2 = ?
                     AND Column3 = ?''', ind)
        address_found = c.fetchall()
        c.execute('''SELECT * FROM table
                     WHERE Column4 = Column5
                     AND Column1  = ?
                     AND Column2 = ?
                     AND Column3 = ?''', ind)
        name_found = c.fetchall() 
        c.execute('''SELECT * FROM table
                     WHERE Column4 < Column5
                     AND Column1 = ?
                     AND Column2 = ?
                     AND Column3 = ?''', ind)
        number_found = c.fetchall()
        if len(number_found) >= 2 or len(address_found) >= 2 or len(name_found) >= 2:
             print(ind)   
             print(otherList[i])                            
             print(len(address_found), len(name_found), len(number_found))