第 0002 題:將 0001 題生成的 200 個(gè)激活碼(或者優(yōu)惠券)保存到 MySQL 關(guān)系型數(shù)據(jù)庫(kù)中。
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')
`