python操作mysql 多参数insert,获取自增id

# coding:utf-8

import configparser
import pymysql
config = configparser.ConfigParser()
config.read('mysql.conf')
lists_header = config.sections()  # 配置组名, ['luzhuo.me', 'mysql'] # 不含'DEFAULT'


host=config['mysql_test']['host']
user=config['mysql_test']['user']
password=config['mysql_test']['password']
port=config['mysql_test']['port']

#获取所有未设置的项目
conn = pymysql.connect(host=host, user=user, passwd=password, db="ssb_ibt",port=3307)
cursor = conn.cursor()

sql="select id from project where import_status=0 and cus_project_id is not null"
cursor.execute(sql)
pids = cursor.fetchall()
#获取需要设置的用户信息
sqlUserInfos="select id,name from ssb_user where `name` in ('windy')"

cursor.execute(sqlUserInfos)
userInfos=cursor.fetchall()


for pid in pids:
    try:
        #插入设置信息
        sqlConfig="insert into config(pid,create_time,creator,if_empty,after_audit) VALUES (%s,now(),1,1,1)"
        cursor.execute(sqlConfig,pid)
        conn.commit()
        #插入步骤信息
        sqlStep="insert into config_step(pid,stage_order,stage_name,step_order,step_name,audit_type,notice_type,`condition`) VALUES (%s,1,'内控',1,'一级审核',3,1,0)"
        cursor.execute(sqlStep, pid)
        stepId=conn.insert_id()
        conn.commit()
        #插入步骤的人员信息
        i = 0
        for user in userInfos:
            sqlStepPerson="insert into config_person(user_id,step_id,user_name,type,`order`) values (%s,%s,%s,1,%s)"
            i = i + 1
            params=(user[0],stepId,user[1],i)
            print(params)
            cursor.execute(sqlStepPerson,params)
            conn.commit()
        #修改项目的是否引入审核模板的状态:未设置==》已设置
        sqlUpdateProject="update project set import_status=1 where id=%s"
        cursor.execute(sqlUpdateProject, pid)
        conn.commit()
    except Exception as e:
        print("error==》", e)
        conn.rollback()
print("项目批量审核设置完毕")

你可能感兴趣的:(Python)