• Python-批量修改


    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    import MySQLdb
    import traceback
    
    # 测试环境脚本 - 批量填充时间线表中的主账号
    
    # UserCenter: 打开数据库连接
    userCenterDB = MySQLdb.connect(host="47.90", port=3306, user="r", passwd="l", db="u", charset='utf8' )
    userCenterCursor = userCenterDB.cursor()
    # Scrm: 打开数据库连接
    scrmDB = MySQLdb.connect(host="47.91", port=3306, user="s", passwd="s", db="s", charset='utf8' )
    scrmCursor = scrmDB.cursor()
    
    try:
        # 查询所有用户
        userCenterCursor.execute("SELECT * from tb_user")
        userCenterResults = userCenterCursor.fetchall()
        for row in userCenterResults:
            # 获取主账号
            userId = row[0]
            parentId = row[1]
            mainUserId = "0"
            if parentId == mainUserId:
                mainUserId = userId
            else:
                mainUserId = parentId
            # 更新主账号到SCRM时间线表
            print u"正在更新用户 userId=%s" % (userId)
            timelineUpdateSQL = "update t_contacts_timeline set main_user_id = '%s' where main_user_id is null and user_id='%s'" % (mainUserId, userId)
            try:
                scrmCursor.execute(timelineUpdateSQL)
                scrmDB.commit()
            except Exception, e:
                print u"更新主账号异常! userId=%s, parentId=%s, mainUserId=%s" % (userId, parentId, mainUserId)
                print u"更新主账号到SCRM-发生异常! msg=%s" % traceback.print_exc()
                # scrmCursor.rollback()
                # scrmDB.rollback()
    
    except Exception,e:
        print u"执行发生异常! msg=%s" % traceback.print_exc()
        userCenterResults.rollback()
        userCenterDB.rollback()
    
    # 关闭数据库连接
    userCenterCursor.close()
    userCenterDB.close()
    scrmCursor.close()
    scrmDB.close()
    print u"执行完成!"
  • 相关阅读:
    A Simple Problem About Truth Table
    Android Fragment完全解析,关于碎片你所需知道的一切
    Android系统中标准Intent的使用
    Android应用开发全程实录-你有多熟悉listView
    Android四大基本组件介绍及生命周期
    设计模式
    Java流(Stream)、文件(File)和IO
    Java网络编程详解
    Java 多线程
    深入理解Java虚拟机 精华总结(面试)
  • 原文地址:https://www.cnblogs.com/wanhua-wu/p/9227431.html
Copyright © 2020-2023  润新知