2014-10-28 2 views
0

다른 쿼리의 결과를 테이블에 어떻게 삽입합니까? 내 코드가 쿼리를 반복하고 결과가 변수에 할당됩니다. 행을 삽입하자마자 결과 집합에 여전히 더 많은 행이 있더라도 코드가 종료됩니다. 이 코드를 계속 유지하고 결과 집합의 모든 행을 삽입하는 올바른 방법은 무엇입니까?파이썬 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)에 대해 작업 (실행)을 수행하고 있습니다. 주위를 둘러 보려고 시도합니다. –

+0

감사합니다. Tim. cur2와 conn2라는 새로운 객체를 만들었습니다. – user1798898

+0

멋지다, 나는 대답과 비슷한 것을 올렸다. 그래서 이것은 "닫힐"수있다. –

답변

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

당신은 당신이 현재 (cur)를 통해 반복하고 그 객체에 대한 조작 (execute)을 수행하고, 그 주위에 방법을 찾아 문제는 사라한다 !