python练习册 每天一个小程序 第0002题
2024-10-17 06:40:52
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()
最新文章
- DBNEWNAME工具介绍
- java基础总结——基础语法1
- 配置Tomcat web保存文件到项目路径之外
- XCODE UITextField 中的属性和用法
- Dev-Cpp配置OpenGL图形库(成功版本:Dev-Cpp 5.7.1 MinGW 4.8.1)
- [转]IIS7.5 添加expires头 提高性能
- hazelcast的坑爹事
- JavaScript 公有 私有 静态属性和方法
- join函数——Gevent源码分析
- opencv2 矩阵方式 resize图像缩放代码(转载)
- mysql show命令
- go web 第一天 学习笔记
- 2015年CSDN博客排名第一名,何方神圣?
- 深度链接(DeepLinking)怎样免费实现
- SCOI2019酱油记
- jquery第二篇
- PHP计算连续签到天数以及累计签到天数
- linux 下crontab -e 命令插入及保存
- Python数据挖掘和机器学习
- Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)