首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python pymssql如何在for循环中插入?

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

Stack Overflow用户
提问于 2014-10-29 03:54:52
回答 1查看 362关注 0票数 0

如何将另一个查询的结果插入到表中?我的代码遍历查询,并将结果赋值给变量。我一插入行,即使结果集中还有更多的行,代码也会终止。让这段代码继续并将所有内容插入到结果集中的行的正确方法是什么?

代码语言:javascript
复制
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()

EN

回答 1

Stack Overflow用户

发布于 2014-10-29 04:17:30

代码语言:javascript
复制
for row in cur:
   #...
   cur.execute (ins_query,(to_db))

您正在对当前正在迭代的对象(cur)执行操作(execute),找到一种绕过该操作的方法,您的问题应该就解决了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26617330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档