import MySQLdb
import string,random
def StepThree(n):
r=[]
/////创建空列表存储数据
s=string.digits+string.letters
for i in range(200):
////循环200次产生200个数据并导入列表
def StepOne():
////生成一个4位的字母数字混合字符串
t = \'\'.join(random.sample(s,4))
return t
tt = \'-\'.join([StepOne() for ii in range(n)])
////将StepOne()函数循环四次产生4个字符串并将其用”-”相连
r.append(tt)
////将生成的数据导入进列表
return r
Host=\'localhost\' //MySQL服务器名
User=\'root\' //数据库使用者
#Password //用户登录密码
Port=80 //数据库使用端口
DB=\'test\' //操作的数据库名
conn = MySQLdb.connect(user=\'root\', db=\'test\', use_unicode=True)
///利用python与MySQL连接的库连接MySQL,此处应有MySQL.connect(host,user,passwd,db)
cur=conn.cursor()
///用来获得Python操作MySQL的方法,实际上是操作游标
r=StepThree(4)
for i in xrange(200):
sql=\'INSERT INTO t1 (id,code) VALUES (%d,\\\'%s\\\');\' % (i+1,r[i])
///完全是SQL语法语句了,INSERT INTO 表名称 VALUES(值1,值2,值3……)
cur.execute(sql)
//真正执行MySQL语句
conn.commit()
///python updata数据时候必须有commit()的提交过程否则不会生效
cur.close()
conn.close()
下一篇:#0001生成验证码