Connector for Python
连接mysql, 需要mysql connector, conntector是一种驱动程序,python连接mysql的驱动程序,mysql官方给出的名称为connector/python,
可参考mysql官网:https://dev.mysql.com/downloads/connector/
环境
操作系统:Windows 10
python版本:2.7
mysql版本:5.5.53 MySQL Community Server (GPL)
IDE工具:pycharm 2016.3.2
接下来列举python操作mysql的步骤:
1.下载并安装connector/python
A.下载mysql-connector-python-2.1.6-py2.7-winx64.msi,下载之后,根据提示安装即可
下载地址:https://dev.mysql.com/downloads/connector/python/
2.使用命令行往mysql中添加数据
A.进入数据库命令行操作界面,使用mysql -u USERNAME -p PASSWORD
B.数据库常用操作
show databases; # 显示所有数据库
create database t1; # 创建数据库t1
use database t1; #指定当前操作的数据库为t1
drop database t1; #删除数据库t1
注:操作数据库,可参考菜鸟教程http://www.runoob.com/mysql/mysql-tutorial.html
C.表中所有数据如下
3.使用python中的mysql.connector模块操作mysql
python代码

import mysql.connector # mysql1.py
config = {
'host': '127.0.0.1',
'user': 'root',
'password': 'root',
'port': 3306,
'database': 'test',
'charset': 'utf8'
}
try:
cnn = mysql.connector.connect(**config)
except mysql.connector.Error as e:
print('connect fails!{}'.format(e))
cursor = cnn.cursor()
try:
sql_query = 'select name,age from stu ;'
cursor.execute(sql_query)
for name, age in cursor:
print (name, age)
except mysql.connector.Error as e:
print('query error!{}'.format(e))
finally:
cursor.close()
cnn.close()

操作结果
(u'xiaoming', 10)
(u'rose', 18)
(u'jack', 19)
(u'fang', 20)
(u'Liang', 40)
(u'Age', None)
更加规范的操作,代码如下

def select2(sql_cmd, param):
"""
:param sql_cmd sql 命令
:param param 参数
"""
try:
conn = mysql.connector.connect(**config)
except mysql.connector.Error as e:
print('connect fails!{}'.format(e)) cursor = conn.cursor()
try:
cursor.execute(sql_cmd, param)
except mysql.connector.Error as e:
print('connect fails!{}'.format(e))
finally:
cursor.close()
conn.close() if __name__ == '__main__':
sql_cmd = "insert into stu (name, age, sex) value (%s, %s, %s)"
param = ('yangguo', 28, 'male')
select2(sql_cmd=sql_cmd, param=param) # 将命令和参数分隔开,操作起来更加安全

Connector for Python的更多相关文章
- connector for python实验
MySQL 是最流行的关系型数据库管理系统,如果你不熟悉 MySQL,可以阅读 MySQL 教程. 下面为大家介绍使用 mysql-connector 来连接使用 MySQL, mysql-conne ...
- Python框架、库以及软件资源汇总
转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...
- Awesome Python
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
- python mysqlDB
1,Description MySQLdb is a Python DB API-2.0-compliant interface Supported versions: * MySQL version ...
- Python开源框架、库、软件和资源大集合
A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome- ...
- 【python】Python框架、库和软件资源大全
很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世界各地的程序员们都能够贡献他们的代码与创新. Python就是这样一门受到全世界各地开源社区支持的语言. ...
- Python 库汇总英文版
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
- Python框架、库和软件资源大全(整理篇)
有少量修改,请访问原始链接.PythonWIn的exe安装包;http://www.lfd.uci.edu/~gohlke/pythonlibs/ 原文链接:codecloud.net/python- ...
随机推荐
- Eureka源码解读
Eureka是我接触分布式软件和服务的第一个框架,所以其原理和实现我的好好研究一下,Eureka可以参看这篇博文:http://springcloud.cn/view/29 初学者会在教程中看到使用@ ...
- Windows下自带压缩文件工具之-makecab
在内网渗透时,当没有rar.7z等压缩工具时候,拖取文件的时候为了防止流量过大,又必须压缩把文件压缩.当然你可以自己上传一个压缩工具.Windows自带制作压缩文件工具makecb你可以了解哈.压缩单 ...
- 【管理篇】用户故事STORY
项目管理中,常常听说story,用户故事 “一开始就做对系统”纯属神话,反之,我们应该去实现今天的用户故事,然后重构,明天再拓展系统.实现新的用户故事.这就是迭代和增量敏捷的精粹所在.
- (转载)CentOS6 Linux系统添加永久静态路由的方法
https://blog.csdn.net/magerguo/article/details/49636231
- python经常使用的十进制、16进制、字符串、字节串之间的转换(长期更新帖)
进行协议解析时.总是会遇到各种各样的数据转换的问题,从二进制到十进制,从字节串到整数等等 废话不多上.直接上样例 整数之间的进制转换: 10进制转16进制: hex(16) ==> 0x10 ...
- Gym 101606F - Flipping Coins - [概率DP]
题目链接:https://codeforc.es/gym/101606/problem/F 题解: 假设 $f[i][j]$ 表示抛 $i$ 次硬币,有 $j$ 个硬币正面朝上的概率. 所以只有两种挑 ...
- NoSuchMethodError 问题
最近maven升级到gradle后,总是报NoSuchMethod error.然后 ,报错的类确实是有这个方法,一切看起来都没有问题.那么运行时jvm到底加载的哪里的类呢?有没有相关的命令可以查询, ...
- SNMP 优秀帖子
-- 比较系统的描述http://blog.sina.com.cn/s/blog_54837cf301011607.html 几个SNMP官方网站(搜索关键字:snmplibrary C#):http ...
- POI导出excel列宽自适应
让单元格宽度随着列和单元格值的宽度自适应: //存储最大列宽 Map<Integer, Integer> maxWidth = new HashMap<>(); // 将列头设 ...
- Cutting Codeforces Round #493 (Div. 2)
Cutting There are a lot of things which could be cut — trees, paper, “the rope”. In this problem you ...