欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)生成隨機數(shù)據(jù)插入mysql數(shù)據(jù)庫的方法

 更新時間:2017年12月25日 13:45:11   作者:金明愛python  
這篇文章主要介紹了Python實現(xiàn)生成隨機數(shù)據(jù)插入mysql數(shù)據(jù)庫的方法,涉及Python隨機字符串生成及數(shù)據(jù)庫連接、插入等相關操作技巧,需要的朋友可以參考下

本文實例講述了Python實現(xiàn)生成隨機數(shù)據(jù)插入mysql數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:

運行結果:

實現(xiàn)代碼:

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)):   #插入數(shù)據(jù)
  passwd=r.choice(passwd1)  #在密碼列表中隨機取一個
  cur.execute("insert into a2(name,passwd) values(%s,%s)",(name[i],passwd))#注意用法
cur.execute('select * from a2') #查詢數(shù)據(jù)
for s in cur.fetchall():
  print(s)
conn.commit()
cur.close()
conn.close()

可見數(shù)據(jù)庫中插入的數(shù)據(jù)有隨機用戶名及其對應密碼。

PS:這里再為大家推薦一款功能相似的在線工具供大家參考使用:

在線隨機生成個人信息數(shù)據(jù)工具:
http://tools.jb51.net/aideddesign/rnd_userinfo

更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學運算技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

相關文章

最新評論