Python Show-Me-the-Code 第 0002 题 MySQL操作
来源:互联网 发布:开票软件赋码 编辑:程序博客网 时间:2024/05/02 01:33
第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
步骤:
- 首先安装MySQLdb,用pip安装即可
- 然后了解下MySQLdb的操作
- 在代码中引入该库
- 连接数据库,写入数据,关闭连接
0002.MySQL操作.py
#!/usr/bin/env python#coding: utf-8import MySQLdbimport gennerate_codeHOST = 'localhost'USER = 'root'PASSWORD = '000ooo'PORT = 3306DB = 'python'#连接数据库conn = MySQLdb.connect( host =HOST, user=USER, passwd=PASSWORD, db=DB, port=PORT)cur = conn.cursor()#生成200组激活码codelist = gennerate_code.generate(200)#将生成的激活码插入到表中for i in xrange(200): sql = 'INSERT INTO code (code) VALUES (\'%s\')' % codelist[i] cur.execute(sql)conn.commit()cur.close()conn.close()
其中gennerate_code是Python Show-Me-the-Code 第 0001 题 生成激活码 中的代码
1 0
- Python Show-Me-the-Code 第 0002 题 MySQL操作
- Python Show-Me-the-Code 第 0003 题 Redis操作
- Python Show-Me-the-Code 第 0002题
- python show-me-the-code 第0004
- Python Show-Me-the-Code 第 0004 题 统计单词
- Python Show-Me-the-Code 第 0021题 密码加密
- Python Show-Me-the-Code 第 0001题
- python show-me-the-code 第0010题
- python show-me-the-code 第0011题
- python show-me-the-code 第0012题
- python操作mysql关系数据库 show me the code 0001-0002
- python 操作 redis --show me the code 0003
- Python Show-Me-the-Code 第 0000 题 图片右上角加数字
- Python Show-Me-the-Code 第 0001 题 生成激活码
- Python Show-Me-the-Code 第 0005 题 批量图片处理
- Python Show-Me-the-Code 第 0006 题 最重要的词
- Python Show-Me-the-Code 第 0008 题 提取HTML正文内容
- Python Show-Me-the-Code 第 0009 题 提取网页中的超链接
- island demo中的combinechildren脚本
- 伪类和为元素
- ImportError: No module named feedparser
- 支付宝充退、充退转账
- 中断服务子程序
- Python Show-Me-the-Code 第 0002 题 MySQL操作
- UIKit---UIButton详解
- 重定位PC相对引用(PC-relative reference relocation)
- SMARTFORMS(小计、合计)
- bzoj 3675: [Apio2014]序列分割
- 7-2通信系统中为什么要进行调制和解调?调制的分类方法有哪些?
- android根据屏幕尺寸区分设备类型,phone或者pad
- Gemcom.Minex.v6.1.1 1CD
- private,空,protected,public 权限