Python实现生成随机数据插入mysql数据库的方法
作者:金明爱python 时间:2024-01-19 09:28:42
本文实例讲述了Python实现生成随机数据插入mysql数据库的方法。分享给大家供大家参考,具体如下:
运行结果:
实现代码:
import random as r
import pymysql
first=('张','王','李','赵','金','艾','单','龚','钱','周','吴','郑','孔','曺','严','华','吕','徐','何')
middle=('芳','军','建','明','辉','芬','红','丽','功')
last=('明','芳','','民','敏','丽','辰','楷','龙','雪','凡','锋','芝','')
name=[]
passwd1=('1234','5678','147','258')
for i in range(101):
name1=r.choice(first)+r.choice(middle)+r.choice(last) #末尾有空格的名字
name2=name1.rstrip() #去掉末尾空格后的名字
if name2 not in name: #名字存入列表中,且没有重名
name.append(name2)
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123',db='test1')
cur = conn.cursor()
for i in range(len(name)): #插入数据
passwd=r.choice(passwd1) #在密码列表中随机取一个
cur.execute("insert into a2(name,passwd) values(%s,%s)",(name[i],passwd))#注意用法
cur.execute('select * from a2') #查询数据
for s in cur.fetchall():
print(s)
conn.commit()
cur.close()
conn.close()
可见数据库中插入的数据有随机用户名及其对应密码。
PS:这里再为大家推荐一款功能相似的在线工具供大家参考使用:
在线随机生成个人信息数据工具:
http://tools.jb51.net/aideddesign/rnd_userinfo
希望本文所述对大家Python程序设计有所帮助。
来源:http://www.cnblogs.com/jmlovepython/p/7388632.html
标签:Python,随机数据,mysql数据库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何使用 SQL Server 数据库查询累计值
2008-12-03 15:27:00
ASP.NET 页面事件执行顺序介绍
2024-05-13 09:17:15
mysql查询优化之100万条数据的一张表优化方案
2024-01-13 20:10:04
JS模拟简易滚动条效果代码(附demo源码)
2024-04-23 09:22:45
![](https://img.aspxhome.com/file/2023/6/135586_0s.png)
mysql报错:MySQL server version for the right syntax to use near type=InnoDB的解决方法
2024-01-20 19:17:17
Keras实现将两个模型连接到一起
2021-07-10 07:24:08
ASP之对象总结
2008-02-27 13:18:00
Python中顺序表原理与实现方法详解
2023-03-24 22:57:48
python中的线程threading.Thread()使用详解
2021-02-25 21:38:38
![](https://img.aspxhome.com/file/2023/5/97395_0s.png)
微信小程序实现翻牌小功能
2023-07-02 05:18:37
python 瀑布线指标编写实例
2023-04-17 02:13:34
详解Python遍历列表时删除元素的正确做法
2023-04-06 18:42:53
![](https://img.aspxhome.com/file/2023/8/133868_0s.png)
PHP后台备份MySQL数据库的源码实例
2023-09-04 14:55:11
python实现简单倒计时功能
2022-09-29 03:08:32
![](https://img.aspxhome.com/file/2023/5/75905_0s.jpg)
纯JS单页面赛车游戏制作代码分享
2024-02-23 14:10:17
![](https://img.aspxhome.com/file/2023/2/56512_0s.jpg)
python实现在cmd窗口显示彩色文字
2023-03-23 22:49:01
![](https://img.aspxhome.com/file/2023/6/131276_0s.jpg)
ASP控制每页打印行数实例
2008-04-13 06:48:00
SQL中distinct的用法(四种示例分析)
2024-01-15 17:18:40
![](https://img.aspxhome.com/file/2023/8/125688_0s.png)
教你pycharm运行Django第一个项目
2021-08-25 16:40:07
![](https://img.aspxhome.com/file/2023/9/80669_0s.png)
PHP适配器模式Adapter Pattern的使用介绍
2023-06-10 12:28:21