zoukankan      html  css  js  c++  java
  • python 往MySQL批量插入数据

    在工作用有时候需要批量造测试数据;手工造太麻烦了,可以通过python批量插入表数据

    '''批量插入sql语句'''
    import pymysql,string,random,time
    def connet_mysql():
        try:
            db=pymysql.connect(host='192.168.31.103',user='root',password='123456',
                               db='test',port=3306)
        except Exception as e:
            print('数据库连接失败',e)
        return db
    
    def insert_data(id,username,password):
        db=connet_mysql()
        cursor=db.cursor()
        sql_1='insert into user_test(id,user,password)values (%s,%s,%s)'
        sql_2='select * from user_test'
        params=(id,username,password)
        cursor.execute(sql_1,params)
        cursor.execute(sql_2)
        db.commit()
        all=cursor.fetchall()#通过游标,获取查询内容
        print(all)
    
    def info():
        str_1d=string.digits
        str_2a=string.ascii_letters
        str_3=str_1d+str_2a
        for i in range(501,601):
            id=i
            username='user'+str(i)
            password=''.join(random.sample(str_3,6))
            insert_data(id,username,password)
    if __name__ == '__main__':
        info()
  • 相关阅读:
    .globl分析
    ARM汇编指令
    汇编文件后缀.s与.S
    Uboot命令
    BIOS、BootLoader、uboot对比
    汇编的WEAK关键字
    USB OTG
    前端学习笔记——CSS选择器
    前端学习笔记——HTML
    【1】python模块:自定义模块的3种导入方式
  • 原文地址:https://www.cnblogs.com/iruance/p/14353731.html
Copyright © 2011-2022 走看看