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

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

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

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

運(yùn)行結(jié)果:

實(shí)現(xiàn)代碼:

import random as r
import pymysql 
first=('張','王','李','趙','金','艾','單','龔','錢','周','吳','鄭','孔','曺','嚴(yán)','華','呂','徐','何')
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: #名字存入列表中,且沒(méi)有重名
    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)  #在密碼列表中隨機(jī)取一個(gè)
  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()

可見(jiàn)數(shù)據(jù)庫(kù)中插入的數(shù)據(jù)有隨機(jī)用戶名及其對(duì)應(yīng)密碼。

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

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

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

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論