#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

__author__ = 'Drake-Z'

import mysql.connector

def write_to_mysql(filename):
    f = open(filename, 'r')
    conn = mysql.connector.connect(user='root', password='******', database='test')
    print('登录数据库成功')
    cursor = conn.cursor()
    cursor.execute("DROP TABLE IF EXISTS user")
    cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
    print('创建表成功')
    a = 1
    for line in f.readlines():
        line = line[:-1]                     #直接除去最后一个字符的所有字符，这里去除\n符号
        cursor.execute('insert into user (id, name) values (%s, %s)', [str(a), line])
        conn.commit()
        a += 1
    cursor.close()
    print('插入数据结束')
    return 0

def search_mysql():
    b = input('查询第几号（1-200）激活码：')
    conn = mysql.connector.connect(user='root', password='******', database='test')
    cursor = conn.cursor()
    cursor.execute('select * from user where id = %s', (b,))
    values = cursor.fetchall()
    print(values)
    cursor.close()
    conn.close()
    return 0

if __name__ == '__main__':
    filename = 'active_code.txt'
    write_to_mysql(filename)
    search_mysql()
@@ -181,36 +181,8 @@
 e):%0A
-    f = open(filename, 'r')%0A
     
@@ -227,38 +227,38 @@
 oot', password='
-******
+986535
 ', database='tes
@@ -265,29 +265,8 @@
 t')%0A
-    print('%E7%99%BB%E5%BD%95%E6%95%B0%E6%8D%AE%E5%BA%93%E6%88%90%E5%8A%9F')%0A
     
@@ -431,61 +431,90 @@
     
-print('%E5%88%9B%E5%BB%BA%E8%A1%A8%E6%88%90%E5%8A%9F')%0A    a = 1%0A    for line in f.readlines(
+f = open(filename, 'r').readlines()%0A    for line, num in zip(f, range(1, len(f)+1)
 ):%0A 
@@ -658,17 +658,19 @@
 ', %5Bstr(
-a
+num
 ), line%5D
@@ -697,23 +697,8 @@
 t()%0A
-        a += 1%0A
     
@@ -716,28 +716,8 @@
 e()%0A
-    print('%E6%8F%92%E5%85%A5%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9D%9F')%0A
     
@@ -765,23 +765,33 @@
 ut('
-%E6%9F%A5%E8%AF%A2%E7%AC%AC%E5%87%A0%E5%8F%B7
+Search Active code
 %EF%BC%881-200%EF%BC%89
-%E6%BF%80%E6%B4%BB%E7%A0%81
 %EF%BC%9A')%0A
@@ -852,14 +852,14 @@
 rd='
-******
+986535
 ', d

Fix 0002