Python pymssql如何在for循环中插入?

时间:2014-10-28 19:54:52

标签: python pymssql

如何从另一个查询中插入表格结果?我的代码遍历查询,结果被分配给变量。一旦插入行,即使结果集中仍有更多行,代码也会终止。让代码继续并将所有内容插入结果集中的行的正确方法是什么?



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




1 个答案:

答案 0 :(得分:0)

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

您正在对当前正在迭代的对象(execute)执行操作(cur),找到解决方法并解决您的问题!