使用python批量生成insert语句的方法
1.建表语句
2.目标insert语句
INSERT INTO `bidprcu_dic_a`( `DIC_ID`, `DIC_TYPE_CODE`, `DIC_TYPE_NAME`, `DATA_DIC_CODE`, `DATA_DIC_NAME`, `PRNT_CODE`, `SRT`, `MEMO`, `DATA_SOUC`, `RID`, `CRTER_ID`, `CRTER_NAME`, `CRTE_OPTINS_NO`, `CRTE_TIME`, `OPT_TIME`, `OPTER_ID`, `OPTER_NAME`, `OPTINS_NO`, `UPDT_TIME`, `INVD_FLAG`) VALUES ('1', 'cert_type', '证件类型', '11', '营业执照', '', NULL, '', NULL, '', NULL, NULL, NULL, '2020-07-07 11:30:39', NULL, NULL, NULL, NULL, '2020-07-07 11:30:39', '0');
3.方法一:python生成insert语句(字符串的拼接)
#生成的目标insert语句 """ INSERT INTO `bidprcu_dic_a`( `DIC_ID`, `DIC_TYPE_CODE`, `DIC_TYPE_NAME`, `DATA_DIC_CODE`, `DATA_DIC_NAME`, `PRNT_CODE`, `SRT`, `MEMO`, `DATA_SOUC`, `RID`, `CRTER_ID`, `CRTER_NAME`, `CRTE_OPTINS_NO`, `CRTE_TIME`, `OPT_TIME`, `OPTER_ID`, `OPTER_NAME`, `OPTINS_NO`, `UPDT_TIME`, `INVD_FLAG`) VALUES ('1', 'cert_type', '证件类型', '11', '营业执照', '', NULL, '', NULL, '', NULL, NULL, NULL, '2020-07-07 11:30:39', NULL, NULL, NULL, NULL, '2020-07-07 11:30:39', '0'); """ import uuid def create_uid(): return str(uuid.uuid1()) f =open('0524insert.txt','w') for i in range(1,3): DIC_ID=create_uid() DIC_TYPE_CODE = "cert_type" DIC_TYPE_NAME="证件类型" DATA_DIC_CODE="11" DATA_DIC_NAME="营业执照" PRNT_CODE="" SRT="NULL" CRTE_TIME="2020-07-07 11:30:39" UPDT_TIME="2020-07-07 11:30:39" INVD_FLAG="0" sql = "INSERT INTO bidprcu_dic_a(DIC_ID,DIC_TYPE_CODE,DIC_TYPE_NAME,DATA_DIC_CODE,DATA_DIC_NAME,PRNT_CODE,SRT,CRTE_TIME,UPDT_TIME,INVD_FLAG) "\ +"VALUES" +"(" +"'"+DIC_ID+"'"+",'"+DIC_TYPE_CODE+"'"+",'"+DIC_TYPE_NAME+"'"+",'"+DATA_DIC_CODE+"'"+",'"+DATA_DIC_NAME+"'"+",'"+PRNT_CODE+"'"+",'"+SRT+"'"+",'"+CRTE_TIME+"'"+",'"+UPDT_TIME+"'"+","+INVD_FLAG+""+")"+";" print(sql) f.write(sql) f.write("\n") f.close()
最终生成数据
INSERT INTO bidprcu_dic_a(DIC_ID,DIC_TYPE_CODE,DIC_TYPE_NAME,DATA_DIC_CODE,DATA_DIC_NAME,PRNT_CODE,SRT,CRTE_TIME,UPDT_TIME,INVD_FLAG) VALUES('6b854a06-bcf9-11eb-8936-e454e805d291','cert_type','证件类型','11','营业执照','','NULL','2020-07-07 11:30:39','2020-07-07 11:30:39',0);
4.方法二:python生成insert语句(列表的方法,value生成后替换)
# 生成的目标insert语句 """ INSERT INTO `bidprcu_dic_a`( `DIC_ID`, `DIC_TYPE_CODE`, `DIC_TYPE_NAME`, `DATA_DIC_CODE`, `DATA_DIC_NAME`, `PRNT_CODE`, `SRT`, `MEMO`, `DATA_SOUC`, `RID`, `CRTER_ID`, `CRTER_NAME`, `CRTE_OPTINS_NO`, `CRTE_TIME`, `OPT_TIME`, `OPTER_ID`, `OPTER_NAME`, `OPTINS_NO`, `UPDT_TIME`, `INVD_FLAG`) VALUES ('1', 'cert_type', '证件类型', '11', '营业执照', '', NULL, '', NULL, '', NULL, NULL, NULL, '2020-07-07 11:30:39', NULL, NULL, NULL, NULL, '2020-07-07 11:30:39', '0'); """ import uuid def create_uid(): return str(uuid.uuid1()) f = open('0524insert.txt', 'w') value = [] for i in range(1, 3): list = [] DIC_ID = list.append(create_uid()) DIC_TYPE_CODE = list.append("cert_type") DIC_TYPE_NAME = list.append("证件类型") DATA_DIC_CODE = list.append(11) DATA_DIC_NAME = list.append("营业执照") PRNT_CODE = list.append("") SRT = list.append("NULL") CRTE_TIME = list.append("2020-07-07 11:30:39") UPDT_TIME = list.append("2020-07-07 11:30:39") INVD_FLAG = list.append(0) print(list) a = value.append(list) c = str(list) print(c) c = c.replace("[", "(").replace("]", ")") print(c) sql = "INSERT INTO bidprcu_dic_a(DIC_ID,DIC_TYPE_CODE,DIC_TYPE_NAME,DATA_DIC_CODE,DATA_DIC_NAME,PRNT_CODE,SRT,CRTE_TIME,UPDT_TIME,INVD_FLAG) " \ + "VALUES" + c + ";" print(sql) f.write(sql) f.write("\n") f.close()
注意:
1.在使用时修改range的值即可
2.在生成大批量数据时,可以考虑多线程【后续学习后,优化该脚本】
到此这篇关于使用python批量生成insert语句的方法的文章就介绍到这了,更多相关python批量生成insert语句内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)