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- ...
随机推荐
- BOM 浏览器对象模型_Storage 接口 - window.sessionStorage - window.localStorage
Storage 接口 用于脚本在浏览器保存数据. 保存的数据都以“键值对”的形式存在.也就是说,每一项数据都有一个键名和对应的值. 所有的数据都是以文本格式保存 受同域限制 ---- 某个网页存入的数 ...
- vue_eHungry 饿了么
eHungry 仿饿了么 git 操作 git checkout -b dev // 创建新分支 dev git push origin dev // 代码推送到 dev ...
- What's the meaning of unqualified-id?
catch( const std::runtime_error & e) { .... } When compile, met an error: error: expected unqual ...
- Java中的常用集合类型总结
1.可重复列表(List) LinkedList和ArrayList的区别:http://www.importnew.com/6629.html ArrayList vs. LinkedList vs ...
- centos7安装nodejs运行环境及卸载
一.安装1.进入官网下载最新版本https://nodejs.org/en/ 选择下载后上传或直接使用wget下载 wget https://nodejs.org/dist/v8.11.2/node- ...
- Redis Keys的通用操作
keys * 显示所有key 127.0.0.1:6379> keys * 1) "sort1" 2) "l2" 3) "set2" ...
- hibernate学习(初识)
hibernate是一个开源的对象关系映射框架(ORM).对JDBC进行了轻量级的封装.将对象和数据库表建立映射关系,hibernate框架使用在数据持久化层(DAO). ORM:对象关系映射(Obj ...
- POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序
题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...
- Windows下Git Bash中VIM打开文件中文乱码
Windows下Git Bash中VIM打开文件中文乱码,解决方法是: 步骤一 admin@DESKTOP-O99620V MINGW64 /d/项目GGE/Hard_for_GGE (master) ...
- 2019-04-15 Python中的面向对象学习总结
一.面向对象总结: (1)三要素:封装,继承,多态 详细介绍链接:https://www.jianshu.com/p/68a ...