study Python 28day (项目002 将淘宝优惠码输入Mysql数据库)

#-*- coding:utf-8 -*-
import uuid
import pymysql

def generateActivationCode(num):
    codeList = []
    for i in range(num):
        code = str(uuid.uuid4()).replace('-','').upper()
        while code in codeList:
            code = str(uuid.uuid4()).replace('-','').upper()
        codeList.append(code)

    return codeList

def storeInMysql(codelist):
    try:
        conn = pymysql.connect(host='127.0.0.1',user='root',passwd='123456',db='mysql')
        cur = conn.cursor()
    except BaseException as e:
        print(e)
    else:
        try:
            cur.execute('CREATE DATABASE IF NOT EXISTS activation_code')
            cur.execute('USE activation_code')
            cur.execute('''CREATE TABLE IF NOT EXISTS code(
                            id INT NOT NULL AUTO_INCREMENT,
                            code VARCHAR(32) NOT NULL,
                            PRIMARY KEY(id)
                        )''')
            for code in codelist:
                cur.execute('INSERT INTO code(code) VALUES(%s)',(code))
                cur.connection.commit()
        except BaseException as e:
            print(e)
    finally:
        cur.close()
        conn.close()

if __name__ == '__main__':
    storeInMysql(generateActivationCode(200))
    print('OK!')
发布了65 篇原创文章 · 获赞 0 · 访问量 541

猜你喜欢

转载自blog.csdn.net/u011624267/article/details/104012320