#!/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"执行完成!"