首页 > 代码库 > python练习册 每天一个小程序 第0002题

python练习册 每天一个小程序 第0002题

 1 #-*-coding:utf-8-*- 
 2 __author__ = Deen 
 3 ‘‘‘
 4 题目描述:
 5     将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
 6 ‘‘‘
 7 """
 8 import MySQLdb as mdb
 9 
10 config = {
11     ‘host‘: ‘127.0.0.1‘,
12     ‘port‘: 3306,
13     ‘user‘: ‘root‘,
14     ‘passwd‘: ‘‘,
15     ‘db‘: ‘student‘,
16     ‘charset‘: ‘utf8‘
17 }
18 conn = mdb.connect(**config)
19 cursor = conn.cursor(cursorclass=mdb.cursors.DictCursor)
20 
21 
22 
23 cursor.execute(‘set names gbk‘)
24 
25 
26 
27 n=0
28 num=list()
29 
30 
31 for line in open("student12.txt"):
32     line=line.decode(‘gbk‘).encode(‘utf-8‘)
33 
34     n=n+1
35 
36     if n%6==1:
37         cursor.execute(‘insert into student12(sno) VALUES (%s)‘%(line))
38 
39 """
40 
41 import MySQLdb as mdb
42 
43 config = {
44     host: 127.0.0.1,
45     port: 3306,
46     user: root,
47     passwd: ‘‘,
48     db: ctf_test,
49     charset: utf8
50 }
51 conn = mdb.connect(**config)
52 cursor = conn.cursor(cursorclass=mdb.cursors.DictCursor)
53 cursor.execute(set names gbk)
54 
55 cursor.execute(create table codes(id int(5),code char(20)))
56 with open(poll_codes.txt,r) as fp:
57     for line in fp.readlines():
58         line = line.strip(\n).split(:)
59         cursor.execute(INSERT INTO codes VALUES (%s,%s),line)
60 
61 
62 conn.commit()
63 cursor.close()
64 conn.close()

 

python练习册 每天一个小程序 第0002题