python批量造数据入库

在这里插入代import pymysql
from faker import Faker#导入Faker类
fake=Faker('zh_CN')#数理化Faker类
class Connect_Sql():
    def __init__(self,host,user,pwd,db,port):
        try:
            self.connet=pymysql.Connect(host=host,user=user,password=pwd,db=db,port=port)
            self.cursor=self.connet.cursor()
        except Exception as e:
            print(e)
    def update(self,sql):
        try:
            self.cursor.execute(sql)
            self.connet.commit()
        except Exception as e:
            print(e)
    def select(self,sql):
        try:
            self.cursor.execute(sql)
            return self.cursor.fetchall()
        except Exception as e:
                print(e)
    def params(self,ids,name,hobby,addr):
        self.cursor.execute(f'insert into new_new11 value({ids},"{name}","{hobby}","{addr}")')
        self.connet.commit()

    def close(self):
        self.cursor.close()#关闭游标
        self.connet.close()#断开连接
if __name__ == '__main__':
    c_sql=Connect_Sql('192.168.31.44','root','123456','class_test',3306)
#     # c_sql.update('delete from faker_user')
#     # c_sql.update('insert into faker_user values("2","大力","123456","广东佛山黄飞鸿")')
#     # print(c_sql.select('select * from faker_user'))
#     # print(fake.name())
#     # print(fake.phone_number())
#     # print(fake.address())
#     # print(fake.building_number())
#     # print(fake.company())
    for i in range(1,1100):
        c_sql.params(i,fake.phone_number(),fake.address(),fake.address())
    print(c_sql.select('select * from new_new11'))
    c_sql.close()

码片
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值