python 生成200个激活码并存进数据库

xiaoxiao2021-02-28  94

Python 练习册,每天一个小程序

第 0001 题: 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?

第 0002 题: 将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。

0000-0010题链接 参考链接:python数据库操作

代码如下:

import random import pymysql def creat_num(num,long): str = 'qwertyuiopasdfghjklzxcvbnm1234567890' b = [] for i in range(num): a = '' for j in range(long): a += random.choice(str) b.append(a) return b def InsertIntoMysql(codelist): # 打开数据库连接 db = pymysql.connect(host='127.0.0.1',user='root',passwd='919824467',db='mysql') # 使用 cursor() 方法创建一个游标对象 cursor cur = db.cursor() #数据库语句 cur.execute('CREATE DATABASE IF NOT EXISTS code') cur.execute('USE code') cur.execute('''CREATE TABLE IF NOT EXISTS num( id INT NOT NULL AUTO_INCREMENT, code VARCHAR(32) NOT NULL, PRIMARY KEY(id) )''') for num in codelist: cur.execute('INSERT INTO num(code) VALUES(%s)',(num)) cur.connection.commit() db.close() InsertIntoMysql(creat_num(200,10))

测试结果如下:

转载请注明原文地址: https://www.6miu.com/read-43472.html

最新回复(0)