连接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的更多相关文章

  1. connector for python实验

    MySQL 是最流行的关系型数据库管理系统,如果你不熟悉 MySQL,可以阅读 MySQL 教程. 下面为大家介绍使用 mysql-connector 来连接使用 MySQL, mysql-conne ...

  2. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

  3. Awesome Python

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

  4. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  5. python mysqlDB

    1,Description MySQLdb is a Python DB API-2.0-compliant interface Supported versions: * MySQL version ...

  6. Python开源框架、库、软件和资源大集合

    A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome- ...

  7. 【python】Python框架、库和软件资源大全

    很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世界各地的程序员们都能够贡献他们的代码与创新. Python就是这样一门受到全世界各地开源社区支持的语言. ...

  8. Python 库汇总英文版

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

  9. Python框架、库和软件资源大全(整理篇)

    有少量修改,请访问原始链接.PythonWIn的exe安装包;http://www.lfd.uci.edu/~gohlke/pythonlibs/ 原文链接:codecloud.net/python- ...

随机推荐

  1. java常用的逻辑

    /** * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com). * <p> * Licensed under th ...

  2. [LeetCode] Split Array With Same Average 分割数组成相同平均值的小数组

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...

  3. PHP使用 strpos() 注意事项

    返回字符出现的第一个位置, 如果字符在被搜索字符串的开头, 则会返回 ‘0’ 因此, 在使用此函数判断 字符串是否包含 某一个字符时  使用: if(strpos('string','str') != ...

  4. c语言二级指针内存模型

    第一种: 指针数组作为输入参数 char *myArray[] = {"aaaaaa", "ccccc", "bbbbbb", " ...

  5. C语言企业级的需要学习的知识

    建立正确程序运行内存的布局图(印象图) 内存四区模型图: 函数调用模型图: 数据类型的本质:固定大小内存块的别名 对于数组变量b[10]; b+1,与&b+1结果不一样: b代表的是数组首元素 ...

  6. MySQL8.0.15的安装与配置---win10

    1.下载地址 https://dev.mysql.com/downloads/installer/ 安装文件:mysql-installer-community-8.0.15.0.msi 2.安装 默 ...

  7. 报文分析5、UDP协议的头结构

    UDP协议的头结构 源端口(2字节) 目的端口(2字节) 封报长度(2字节) 校验和(2字节) 数据 (1)源端口(Source Port):16位的源端口域包含初始化通信的端口号.源端口和IP地址的 ...

  8. [dev] udp socket的read长度问题

    场景描述 我的两个程序需要彼此通信.采用unix socket来实现. 并为了简单起见使用了DGRAM,也就是udp通信. 问题描述 1. 用法是这样的 收包的一端使用epoll监听,发包端发送一个2 ...

  9. HTML5 页面编辑API之Range对象

    在 HTML5 中,一个 Range 对象代表页面上的一段连续区域.通过 Range 对象,可以获取或修改页面上的任何区域.包含获取,修改,删除和替换等操作. 一:获取range对象的值 Range对 ...

  10. bug和注意事项

    bug: 1.新增角色,在选择权限树的时候,如果不选择根目录下的第一个节点,保存后,权限树会打不开. 2.文档页面有两个大字段,即ueditor编辑器的时候,保存后回显会有问题 不过一个页面有两个大文 ...