簡體   English   中英

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

[英]Python pymssql how do I insert in a for loop?

如何將另一個查詢的結果插入表中? 我的代碼遍歷查詢,並將結果分配給變量。 一旦插入行,即使結果集中還有更多行,代碼也會終止。 使該代碼繼續並將所有內容插入結果集中的行的正確方法是什么?

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

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

您正在對當前正在迭代的對象( cur )執行操作( execute ),找到解決該問題的方法,那么問題就應該消除了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM