github上面的题目、拿来练手(产生序列号并存入mysql&redis中)
#!/usr/bin/env python#-*- coding:utf-8 -*-
__author__ = "richardGaoPy"
import random
import string
COUPON_NUM = 200
COUPON_STR_LENGTH = 8
def mysqlopt(lst):
try:
import MySQLdb
db = MySQLdb.connect("127.0.0.1",'root','bigbird','remy')
cursor = db.cursor()
cursor.execute("create table zcmgl(id int primary key,regist_num varchar(8))")
#i=1
for i in xrange(0,200):
cursor.execute("insert into zcmgl values(%s,%s)",(i,lst))
db.commit()
cursor.close()
db.close()
except:
print "Save data failed!"
def redisopt(lst):
"""对redis的操作非常不熟悉、要加强一下;建立redis连接写入数据"""
try:
import redis
r = redis.StrictRedis(host="localhost",port="6379",db=0)
for x in xrange(0,200):
r.set(x,lst)
except:
print "Save data failed to redis."
if __name__ == "__main__":
"""随机产生注册码、并存入mysql和redis缓存中"""
lst = list(string.letters) + list(string.digits)
zc_lst = []
for i in xrange(COUPON_NUM):
coupon_list =
coupon_str = "".join(coupon_list)
zc_lst.append(coupon_str)
mysqlopt(zc_lst)
redisopt(zc_lst)
页:
[1]