第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
sudo apt-get install mysql-server mysql-client
import mysql.connector
def store_mysql(file_path):
conn = mysql.connector.connect(user='root', password='123456')
cursor = conn.cursor()
cursor.execute('create database if not exists ShowMeTheCode;')
cursor.execute('create table \
if not exists \
ShowMeTheCode.active_code(id int(4) not null primary key auto_increment, \
code char(20) not null);')
with open(file_path) as f:
for line in f.readlines():
code = line.strip()
cursor.execute('insert into ShowMeTheCode.active_code (code) values (%s);', [code])
conn.commit()
cursor.close()
conn.close()
if name == "main":
store_mysql('active_code.txt')
`