首页 > 其他分享 >tj-factory_Person_v1_to_v2.py

tj-factory_Person_v1_to_v2.py

时间:2023-05-24 20:11:24浏览次数:30  
标签:dept post py factory cursor v1 185 line id

说明:该脚本把21的mysql数据库factory_cloud.personnel表里的数据迁移到185的mysql数据库tj_factory_prod.bd_person表里,这2个表字段不一致,只要2个表相关联的字段。

 

import pymysql
import sys
import datetime
from time import strftime
##################在21找出人员数据######################
try:
    mysql_connetion_21 = pymysql.connect(host='192.168.1.21',user='project_ifactorys_manager',passwd='88a78967-a282-4166-a0e8-ee409f893935',port=3306)
except:
        print("mysql连接失败!") 
        sys.exit(1)
select_sql = "select name,gender,birthday,phone,credentials_type,credentials_number,starttime,endtime,sync_status,is_black,enable_state,black_time,worker_name,depart_code from factory_cloud.personnel ;"
cursor_21 = mysql_connetion_21.cursor()
cursor_21.execute(select_sql)
results_21 = list(cursor_21.fetchall())


now_datetime = strftime("%Y-%m-%d %H:%M:%S")
print(now_datetime)
print(results_21)
#-------------------在21找出人员数据-------------------------------

####################在185的bd_person表插入数据##########################################
mysql_connetion_185 = pymysql.connect(host='172.16.13.185',user='root',passwd='sykj_2022',port=3306)
cursor_185 = mysql_connetion_185.cursor()
for line in results_21:
    line = list(line)
    print(line)
    # 证件类型
    line[4] = line[4] + 1
    # 是否可用
    if int(line[10]) == 0:
        line[10] = 2
    elif int(line[10]) == 1:
        line[10] = 0
    # 黑名单创建时间
    
    # insert_sql = 'insert into tj_factory_prod.bd_person(name,type,sex,birthday,phone,credentials_type,credentials_number,effective_begin,effective_end,sync_status,sync_message,is_black,step_val,del_flag,create_by,update_by,create_time,user_id,dept_id,black_time) values("{}",{},{},"{}","{}",{},"{}","{}","{}",{},"{}",{},{},{},"{}","{}","{}",{},{},"{}")'.format(line[0],1,line[1],line[2],line[3],line[4],line[5],line[6],line[7],0,'未开始',line[9],1,line[10],'admin','admin',now_datetime,1,100,line[11])
    insert_sql = 'insert into tj_factory_prod.bd_person(name,type,sex,birthday,phone,credentials_type,credentials_number,effective_begin,effective_end,sync_status,sync_message,is_black,step_val,del_flag,create_by,update_by,create_time,user_id,dept_id,work_type) values("{}",{},{},"{}","{}",{},"{}","{}","{}",{},"{}",{},{},{},"{}","{}","{}",{},{},"{}")'.format(line[0],1,line[1],line[2],line[3],line[4],line[5],line[6],line[7],1,'同步成功',line[9],1,line[10],'admin','admin',now_datetime,1,100,'钢筋工')
    # print(insert_sql)
    try:
        cursor_185.execute(insert_sql)
        mysql_connetion_185.commit()
    except Exception as e:
        print(e)
        print("插入bd_person表失败")
        sys.exit(1)
 #-----------------------#在185的bd_person表插入数据----------------------------------------------------   
 
 ####################在185的bd_person_dept_post表插入数据##########################################
    #根据身份证号码找person 人的id
    cursor_185.execute('select id from tj_factory_prod.bd_person where credentials_number="{}";'.format(line[5]))
    person_id = cursor_185.fetchone()[0]
    # print('select id from tj_factory_prod.bd_person where credentials_number="{}";'.format(line[5]))
    
    #根据岗位名称找post_id  岗位id
    post_select = 'select post_id from tj_factory_prod.sys_post where post_name = "{}";'.format(str(line[12]))
    cursor_185.execute(post_select)
    post = cursor_185.fetchone()
    if not post:
        post_id = 4
    else:
        post_id = post[0]
        
    #在21根据depart_code在base_depart表找name 部门名称
    depart_select = 'select name from factory_pms.base_depart where code="{}";'.format(str(line[13]))
    cursor_21.execute(depart_select)
    depart = cursor_21.fetchone()
    if depart:
        depart_name = depart[0]
    else:
        depart_name = '自有工人'
    #根据部门名称找dept_id 部门id
    dept_select = 'select dept_id from tj_factory_prod.sys_dept where dept_name = "{}" and del_flag = 0;'.format(str(depart_name))
    cursor_185.execute(dept_select)
    dept = cursor_185.fetchone()
    if not dept:
        dept_id = 223
    else:
        dept_id = dept[0]
        
    insert_sql = "insert into tj_factory_prod.bd_person_dept_post(person_id,dept_id,post_id,is_main) values({},{},{},1);".format(person_id,dept_id,post_id)
    try:
        cursor_185.execute(insert_sql)
        mysql_connetion_185.commit()
    except Exception as e:
        print(e)
        print("插入bd_person_dept_post表失败")
        sys.exit(1)
 #------------------------在185的bd_person_dept_post表插入数据----------------------------------------
        
cursor_185.close()
    
mysql_connetion_185.close()
    
cursor_21.close()
mysql_connetion_21.close()

 

标签:dept,post,py,factory,cursor,v1,185,line,id
From: https://www.cnblogs.com/lfxx/p/17429377.html

相关文章

  • python turtle 海龟绘图,绘制小猪佩奇
    项目介绍:瞎玩的,要用Python来画小猪佩奇。其实这个实现并不难,只要使用Python的内置模块turtle进行绘图即可。但是,如要完成一个好的作品,还是需要耗费一定时间的,因为你要提前布置好所有点的坐标和走线样式等,以及实际测试中会反复调试也比较麻烦。下面就是最终完成的小猪佩琪效果动态图......
  • Python——datetime库
    Python——datetime库datetime是Python内置的一个处理日期和时间的标准库,可以轻松处理日期和时间,也可以进行日期和时间的格式化操作。下面是一些datetime库中常用的方法:datetime.date:返回表示日期的对象。datetime.time:返回表示时间的对象。datetime.datetime:返回日期和时......
  • NumPy学习2
    继续学习NumPynumpy_test2.py:importnumpyimportnumpyasnp'''4,Numpy创建数组除了使用array()方法外,NumPy还提供了其他创建ndarray数组的方法。1)numpy.empty()numpy.empty()创建未初始化的数组,可以指定创建数组的形状(shape)和数据类型(dtype),语法格式如下:num......
  • python代码热更新原理
    python代码热更新原理热更新概念在进程不重启的前提下,修改代码并且使得修改的代码生效热更新背景需求紧急修复线上问题实现不停机维护要实现上面的用户需求,需要在原理上支持下面需求*1.支持任意的import语法并且无顺序依赖要求2.对应回调函数、已实例化对象等也要支持代码......
  • Python3源码编译和使用静态链接库lib&动态链接库dll详细介绍
    说明:本文主要介绍:1.python3源码的下载和编译内核工程(pythoncore)2.编译的lib和dll使用例子。若需要编译源码PCBuild下其他python工程,类似参考内核工程(pythoncore)编译的方式进行配置即可。一.python源码下载1.进入python官方网站:PythonSourceReleases|Python.org2.选择......
  • 边缘计算AI硬件智能分析网关V1版的接入流程与使用步骤
    我们的AI边缘计算网关硬件——智能分析网关目前有两个版本:V1版与V2版,两个版本都能实现对监控视频的智能识别和分析,支持抓拍、记录、告警等,在AI算法的种类上和视频接入上,两个版本存在些许的区别。V1的基础算法有人体检测、区域入侵检测、戴口罩识别、安全帽识别;V2目前有15种算法,包括......
  • Pytest - 断言判断(2) - 断言失败继续执行(pytest-assume)
    断言失败继续执行前言一般情况下我们在使用assert断言失败后,后面的代码就不会继续运行;如果我们想要在断言失败后想要继续运行代码,就不能使用assert进行验证;一个可以允许pytest测试用例中,执行多个失败的断言的插件:多重断言pytest-assume安装pipinstallpytest......
  • pycharm中创建python包失效
    testDemo是我创建的python包,但图标一直显示为文件夹,导致后期使用无法导入。   解决: 在设置中找到项目结构 选择对应文件夹并标记为源即可......
  • Pytest - pytest 命令(1) - 命令执行方法
    命令执行方法讲解下pytest分别在Windows,Linux,Pycharm中,执行pytest的方法;追加的参数可以参考:Pytest-pytest命令(2)-命令参数及含义Pytest-pytest命令(3)-常用命令的使用Windows执行Windows下执行pytest测试脚本没什么难度,在用例的目录下打开cmd窗口......
  • 002 Python 学习笔记
    #通过:f"内容{变量}"的格式,实现快速格式化,如下示例print("-----------字符串快速格式化------------")name="sunwugang"age=18print(f"mynameis{name},ageis{age}")print("mynameis{},ageis{}".format(name,age)) ......