连接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. 最大子矩阵和问题dp

    给定一个矩阵 matrix,其中矩阵中的元素可以包含正数.负数.和0,返回子矩阵的最大累加和.例如,矩阵 matrix 为: 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 - ...

  2. [LeetCode] Serialize and Deserialize N-ary Tree N叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. Log4j的入门和使用

    Log4j(log for java)是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件,甚至是套接口服务器.NT的事件记录器.UNIX Sy ...

  4. duilib 新增数据迁移界面

    xml界面配置: <?xml version="1.0" encoding="utf-8"?> <Window caption="0 ...

  5. Jenkins调度Selenium脚本不能打开浏览器解决办法

    前提:在Myeclipse里面可以启动起来浏览器,在Jenkins中不能启动浏览器 原因:以程序的方式安装了jenkins,jenkins就成了windows的一个服务了,默认是设置为自动启动的如下图 ...

  6. windows创建定时任务执行python脚本

    一.创建定时任务 \ [程序或脚本]文本框中填的是Python编译器的名称,一般就是python.exe, [起始于]文本框中填的是Python编译器的目录,上图中假设你的Python编译器的完整路径 ...

  7. Apache Arrow

    https://www.kdnuggets.com/2017/02/apache-arrow-parquet-columnar-data.html https://arrow.apache.org/ ...

  8. Source Insight相关设置

    #Source Insight中按快捷键在其他编辑器中打开当前文件 "D:\Program Files\Zend\ZendStudio-5.5.0\bin\ZDE.exe"  %f ...

  9. docker安装,err:exit status 255,提示找不到虚拟机IP

    我遇到这个问题是因为,BIOS没有打开虚拟化技术,导致虚拟机无法成功创建,自然找不到IP. 解决: 1.进入BIOS,高级选项卡下,找到虚拟化技术开关,打开即可.具体做法,可搜网文. 2.删除原来自动 ...

  10. [vue开发记录]vue仿ios原生datepicker实现

    先上个效果图 现在只开发了年月,还在优化. 在网上看了一个纯原生js实现实现惯性滚动和回弹的文章  地址:https://www.cnblogs.com/ranyonsue/p/8119155.htm ...