简体   繁体   中英

Python pymssql how do I insert in a for loop?

How do I insert into a table results from another query? My code loops through the query and the results are assigned to variables. As soon as I insert the row the code terminates even though there are still more rows in the result set. What is the proper way to have this code continue and insert all to rows from the result set?

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

You're performing an operation ( execute ) on the object that you're currently iterating over ( cur ), find a way around that and your problem should be gone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM