在这里插入代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()
码片
python批量造数据入库
最新推荐文章于 2024-01-03 14:24:01 发布