python制造虚拟姓名电话保存到mysql数据库

时间:2024-04-10 18:40:30
#启动命令:1、python test.py import os # 开始-需要安装的模块列表 #需要安装的库 libs = ["requests","mysql-connector-python"] #循环遍历安装 for lib in libs: os.system("pip install -i https://pypi.tuna.tsinghua.edu.cn/simple " + lib) # import import mysql.connector import random #生产名字和电话 def random_name(): # 删减部分,比较大众化姓氏 firstName = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻水云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳鲍史唐费岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅卞齐康伍余元卜顾孟平" \ "黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计成戴宋茅庞熊纪舒屈项祝董粱杜阮席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田胡凌霍万柯卢莫房缪干解应宗丁宣邓郁单杭洪包诸左石崔吉" \ "龚程邢滑裴陆荣翁荀羊甄家封芮储靳邴松井富乌焦巴弓牧隗山谷车侯伊宁仇祖武符刘景詹束龙叶幸司韶黎乔苍双闻莘劳逄姬冉宰桂牛寿通边燕冀尚农温庄晏瞿茹习鱼容向古戈终居衡步都耿满弘国文东殴沃曾关红游盖益桓公晋楚闫" # 百家姓中双姓氏 firstName2 = "万俟司马上官欧阳夏侯诸葛闻人东方赫连皇甫尉迟公羊澹台公冶宗政濮阳淳于单于太叔申屠公孙仲孙轩辕令狐钟离宇文长孙慕容鲜于闾丘司徒司空亓官司寇仉督子颛孙端木巫马公西漆雕乐正壤驷公良拓跋夹谷宰父谷梁段干百里东郭南门呼延羊舌微生梁丘左丘东门西门南宫南宫" # 女孩名字 girl = '秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽' # 男孩名字 boy = '伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘' # 名 name = '中笑贝凯歌易仁器义礼智信友上都卡被好无九加电金马钰玉忠孝' # 10%的机遇生成双数姓氏 if random.choice(range(100)) > 10: firstName_name = firstName[random.choice(range(len(firstName)))] else: i = random.choice(range(len(firstName2))) firstName_name = firstName2[i:i + 2] sex = random.choice(range(2)) name_1 = "" # 生成并返回一个名字 if sex > 0: girl_name = girl[random.choice(range(len(girl)))] if random.choice(range(2)) > 0: name_1 = name[random.choice(range(len(name)))] return firstName_name + name_1 + girl_name else: boy_name = boy[random.choice(range(len(boy)))] if random.choice(range(2)) > 0: name_1 = name[random.choice(range(len(name)))] return firstName_name + name_1 + boy_name def create_sfz(): # 第二位数字 second = [3, 4, 5, 7, 8][random.randint(0, 4)] # 第三位数字 third = { 3: random.randint(0, 9), 4: [5, 7, 9][random.randint(0, 2)], 5: [i for i in range(10) if i != 4][random.randint(0, 8)], 7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)], 8: random.randint(0, 9), }[second] # 最后八位数字 suffix = random.randint(9999999,100000000) suffix7 = random.randint(999999, 10000000) # 拼接手机号 return "1{}{}{}{}".format(second, third, suffix,suffix7) def create_phone(): # 第二位数字 second = [3, 4, 5, 7, 8][random.randint(0, 4)] # 第三位数字 third = { 3: random.randint(0, 9), 4: [5, 7, 9][random.randint(0, 2)], 5: [i for i in range(10) if i != 4][random.randint(0, 8)], 7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)], 8: random.randint(0, 9), }[second] # 最后八位数字 suffix = random.randint(9999999,100000000) # 拼接手机号 return "1{}{}{}".format(second, third, suffix) #创建多个姓名、身份证号、与电话号码 # def create_nameAndIdCardAndPhoneNumber(length): # person_info_list = list() # for _ in range(length): # name = random_name() # id_card = create_sfz() # phone = create_phone() # person_info_list.append((name, id_card, phone)) # return person_info_list # # 创建10个人的姓名、身份证号和电话号码 # persons = create_nameAndIdCardAndPhoneNumber(10) # # 打印个人信息 # for name, id_card, phone in persons: # print(f"name: {name}, idCard: {id_card}, phoneNumber: {phone}") # 开始----正式脚本 #1. 创建数据库连接 db = mysql.connector.connect( host="xxxx", # MySQL服务器地址 user="xxx", # 用户名 password="xxxx", # 密码 database="xxxxx" # 数据库名称 ) try: # 开启事务 db.start_transaction() # 创建游标对象,用于执行SQL查询 cursor = db.cursor() # 查询所有记录 cursor.execute("SELECT id,grid_code,grid_name,head,head_phone FROM forestry_management.grid_data_management") # 获取查询结果 results = cursor.fetchall() resultsLength = len(results) for row in results: id,grid_code,grid_name,head,head_phone = row print(f"id:{id},grid_code:{grid_code},grid_name:{grid_name},head:{head},head_phone:{head_phone}") name = random_name() id_card = create_sfz() phone = create_phone() sql = "update grid_data_management set head='%s',head_phone='%s' where id='%s'" % (name,phone,id) # 执行SQL语句 cursor.execute(sql) print("Update data list,The number of results is %s" % (resultsLength)); db.commit() # 提交事务 except Exception as e: db.rollback() # 事务回滚,撤销之前的操作 print("Transaction rolled back:", e) finally: # 关闭游标和数据库连接 cursor.close() db.close() print('end :completion of enforcement')