2017-09-14 54 views
1

我試圖做一個查詢從一個服務器採取了一些數據,但我現在想使多個查詢和多個連接,這就是問題所在:Python的字典用pyodbc

我想打一個爲,通過與我的所有連接,救救我「IN」的條件爲每一位:

if len(su_dict['dst_srv']) > 1: 
    su_dict['skill'].clear() 
    su_dict['vdn'].clear() 
    i = 0 
    for x in range(len(su_dict['dst_srv'])): 
     skill_varios = {} 
     sql = ("SELECT skill FROM info_skill_vdn_campana WHERE activo = 1 AND pbx = 'V2' AND dst_srv = '"+str(list(su_dict['dst_srv'])[i])+"';") 
     skill = crsr_kala.execute(sql).fetchall() 
     skill_varios[str(list(su_dict['dst_srv'])[i])] = ', '.join([x[0] for x in skill]) 
     i += 1 
    print skill_varios 

這裏的問題是,當我在屏幕上打印我的結果它僅保存最後在狀態,但我不知道爲什麼...

這是該詞典的結果:

{'172.59.80.101': '2474, 2475, 2477, 2476'} 

但必須是這一點,因爲我有2個服務器:

{'172.59.80.01': '2474, 2475, 2477, 2476' '172.80.52.31': '54, 60, 80, 787'} 

回答

0

你設定的字典是for循環中的空白,所以將只包含最後一個記錄集。試試這個:

if len(su_dict['dst_srv']) > 1: 
    su_dict['skill'].clear() 
    su_dict['vdn'].clear() 
    i = 0 
    skill_varios = {} 

    for x in range(len(su_dict['dst_srv'])): 
     sql = ("SELECT skill FROM info_skill_vdn_campana WHERE activo = 1 AND pbx = 'V2' AND dst_srv = '"+str(list(su_dict['dst_srv'])[i])+"';") 
     skill = crsr_kala.execute(sql).fetchall() 
     skill_varios[str(list(su_dict['dst_srv'])[i])] = ', '.join([x[0] for x in skill]) 
     i += 1 

    print(skill_varios) 

祝你好運!

+0

此代碼包含潛在的SQL注入漏洞。 – RegularlyScheduledProgramming