2014-10-28 19 views
0

如何從另一個查詢插入到表中?我的代碼循環查詢並將結果分配給變量。只要插入行,即使結果集中還有更多行,代碼也會終止。讓代碼繼續並將所有行插入結果集中的行的正確方法是什麼?Python pymssql如何在for循環中插入?

cur.execute (query3) 
 

 
for row in cur: 
 
    calldate= row['calldate'] 
 
    account= row['account'] 
 
    call_time= row['Call_Time'] 
 
    distributor_name= row['distributor_name'] 
 
    callerid= row['callerid'] 
 
    call_status= row['call_status'] 
 
    partner_revenue= row['partner_revenue'] 
 
    tracking_phone= row['tracking_phone'] 
 
    quality_string= row['quality_string'] 
 
    column9= row['column 9'] 
 
    column10= row['column 10'] 
 
    column11= row['column 11'] 
 
    column12= row['column 12'] 
 

 
    if column9 is None and column10 is None and column11 is None and column12 is None: 
 
     to_db = calldate, account, call_time, distributor_name,callerid, call_status, partner_revenue, tracking_phone, quality_string 
 

 
    if column9 is not None and column10 is None and column11 is None and column12 is None: 
 
     to_db = calldate, account, call_time, distributor_name,callerid, call_status, partner_revenue, tracking_phone, quality_string +", "+ column9 
 

 
    if column9 is not None and column10 is not None and column11 is None and column12 is None: 
 
     to_db = calldate, account, call_time, distributor_name,callerid, call_status, partner_revenue, tracking_phone, quality_string +", "+ column9 +", "+ column10 
 

 
    if column9 is not None and column10 is not None and column11 is not None and column12 is None: 
 
     to_db = calldate, account, call_time, distributor_name,callerid, call_status, partner_revenue, tracking_phone, quality_string +", "+ column9 +", "+ column10 +", "+ column11 
 

 
    if column9 is not None and column10 is not None and column11 is not None and column12 is not None: 
 
     to_db = calldate, account, call_time, distributor_name,callerid, call_status, partner_revenue, tracking_phone, quality_string +", "+ column9 +", "+ column10 +", "+ column11 +", "+ column12 
 
    
 
    cur.execute (ins_query,(to_db)) 
 
    conn.commit()

+0

您正在對當前正在迭代的對象(cur)執行操作(執行),試圖找到解決方法 – 2014-10-28 20:04:19

+0

感謝Tim。我創建了一個新的對象cur2和conn2,並工作。 – user1798898 2014-10-28 20:15:41

+0

酷,我發佈了類似的答案,所以這可以「關閉」 – 2014-10-28 20:17:52

回答

0
for row in cur: 
    #... 
    cur.execute (ins_query,(to_db)) 

你,你當前迭代(cur)執行的對象進行操作(execute),發現周圍的一種方法,你的問題應該消失!