我想好了再告诉你 發表於 2019-5-22 15:20:00

python接口自动化(三十八)-python操作mysql数据库(详解)

<h2>简介</h2>
<p>  现在的招聘要求对QA人员的要求越来越高,测试的一些基础知识就不必说了,来说测试知识以外的,会不会一门或者多门开发与语言,能不能读懂代码,会不会Linux,会不会搭建测试系统,会不会常用的数据库,会不会SQL等等,因此我们这篇文章来讲解如何用</p>
<p>python语言操作mysql数据库。</p>
<p>&nbsp;本科阶段曾学过使用java对MySQL数据库进行操作,基本思路是先连接数据库,然后执行SQL语句对数据库进行操作,最后打印结果并断开连接。使用Python操作数据库的流程和以上过程基本一致,在对其进行介绍之前,先介绍一些基本的概念。</p>
<h2>基本概念</h2>
<h3>python操作数据库的流程</h3>
<p>以流程图的方式展示python操作MySQL数据库的流程:</p>
<p><img style="display: block; margin-left: auto; margin-right: auto" src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190523083805321-664856846.png" alt=""></p>
<p>  对上图的解读:首先检查是否依次创建Connection对象(数据库连接对象)用于打开数据库连接,创建Cursor对象(游标对象)用于执行查询和获取结果;然后执行SQL语句对数据库进行增删改查等操作并提交事务,此过程如果出现异常则使用回滚技术使数据库恢</p>
<p>复到执行SQL语句之前的状态;最后,依次销毁Cursor对象和Connection对象,以避免多计算机内存过多的占用和浪费。</p>
<p>  下面依次对Connection对象、Cursor对象和事务等概念进行介绍。</p>
<h3>Connection对象</h3>
<p>Connection对象即为数据库连接对象,在python中可以使用pymysql.connect()方法创建Connection对象,该方法的常用参数如下:</p>
<p>host:连接的数据库服务器主机名,默认为本地主机(localhost);字符串类型(String) 。</p>
<p>user:用户名,默认为当前用户;字符串类型(String) 。</p>
<p>passwd:密码,无默认值;字符串类 (String)。</p>
<p>db:数据库名称,无默认值;字符串类型(String) 。</p>
<p>port:指定数据库服务器的连接端口,默认为3306;整型(int)。</p>
<p>charset:连接字符集;字符串类型(String)</p>
<h4>Connection对象常用的方法如下:</h4>
<p>cursor():使用当前连接创建并返回游标 。</p>
<p>commit():提交当前事务 。</p>
<p>rollback():回滚当前事务 。</p>
<p>close():关闭当前连接</p>
<h3>Cursor对象</h3>
<p>Cursor对象即为游标对象,用于执行查询和获取结果,在python中可以使用conn.cursor()创建,conn为Connection对象。Cursor对象常用的方法和属性如下:</p>
<p>execute():执行数据库查询或命令,将结果从数据库获取到客户端 fetchone():获取结果集的下一行 fetchmany():获取结果集的下几行 fetchall():获取结果集中剩下的所有行 close():关闭当前游标对象 rowcount:最近一次的execute返回数据的行数或受影响的行数</p>
<h2>事务</h2>
<p>1、事务机制可以确保数据一致性。</p>
<p>事务是数据库理论中一个比较重要的概念,指访问和更新数据库的一个程序执行单元,具有ACID特性:</p>
<ul>
<li>原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。</li>
<li>一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。</li>
<li>隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。</li>
<li>持久性(durability)。持续性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。</li>
</ul>
<p>在开发时,我们以以下三种方式使用事务:</p>
<p>正常结束事务:conn.commit() 异常结束事务:conn.rollback() 关闭自动commit:设置 conn.autocommit(False)</p>
<p>2 、使用python实现对MySQL数据库的增删改查等操作</p>
<p>在python中操作MySQL数据库时,要使用的模块是:</p>
<p>Python2中:mysqldb(pip2 install mysqldb) Python3中:pymysql(pip3 install pymysql)</p>
<p>本篇博客所使用的环境为:</p>
<p>python 3.7 win 10 pycharm 2018.3.5 pymysql 0.9.2 mysql 5.6&nbsp;</p>
<p>下面将以具体代码的形式依次介绍python中如何实现对MySQL数据库的增删改查等操作。</p>
<h2>python操作数据库</h2>
<p>Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口。</p>
<p>Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库:</p>
<ul>
<li>GadFly</li>
<li>mSQL</li>
<li>MySQL(小公司、小厂首选)</li>
<li>PostgreSQL</li>
<li>Microsoft SQL Server 2008(中型厂、中等公司首选)</li>
<li>Informix</li>
<li>Interbase</li>
<li>Oracle(大厂、大企业首选)</li>
<li>Sybase</li>
</ul>
<p>你可以访问Python数据库接口及API查看详细的支持数据库列表。</p>
<p>不同的数据库你需要下载不同的DB API模块,例如你需要访问Oracle数据库和Mysql数据,你需要下载Oracle和MySQL数据库模块。</p>
<p>DB-API 是一个规范. 它定义了一系列必须的对象和数据库存取方式, 以便为各种各样的底层数据库系统和多种多样的数据库接口程序提供一致的访问接口 。</p>
<p>Python的DB-API,为大多数的数据库实现了接口,使用它连接各数据库后,就可以用相同的方式操作各数据库。</p>
<h2>Python DB-API使用流程:</h2>
<ul class="list">
<li>引入 API 模块。</li>
<li>获取与数据库的连接。</li>
<li>执行SQL语句和存储过程。</li>
<li>关闭数据库连接。</li>
</ul>
<hr>
<h2>什么是MySQLdb?</h2>
<p>PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。</p>
<p>PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。</p>
<h2>如何安装MySQLdb?</h2>
<p>在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装。</p>
<p>PyMySQL 下载地址:https://github.com/PyMySQL/PyMySQL。</p>
<p>如果还未安装,我们可以使用以下命令安装最新版的 PyMySQL:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> pip install PyMySQL</pre>
</div>
<p>&nbsp;<img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522134717229-660691548.png" alt=""></p>
<p>如果你的系统不支持 pip 命令,可以使用以下方式安装:</p>
<p>1、使用 git 命令下载安装包安装(你也可以手动下载):</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> git clone https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">github.com/PyMySQL/PyMySQL</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> cd PyMySQL/
<span style="color: rgba(0, 128, 128, 1)">3</span> python3 setup.py install</pre>
</div>
<p>2、如果需要制定版本号,可以使用 curl 命令来安装:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)"># X.X 为 PyMySQL 的版本号
</span><span style="color: rgba(0, 128, 128, 1)">2</span> url -L https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">github.com/PyMySQL/PyMySQL/tarball/pymysql-X.X | tar xz</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> cd PyMySQL*
<span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 0, 1)">python3 setup.py install
</span><span style="color: rgba(0, 128, 128, 1)">5</span> # 现在你可以删除 PyMySQL* 目录</pre>
</div>
<p><strong>注意:</strong>请确保您有root权限来安装上述模块。</p>
<blockquote>
<p>安装的过程中可能会出现"ImportError: No module named setuptools"的错误提示,意思是你没有安装setuptools,你可以访问https://pypi.python.org/pypi/setuptools&nbsp;找到各个系统的安装方法。</p>
<p>Linux 系统安装实例:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> $ wget https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">bootstrap.pypa.io/ez_setup.py</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> $ python3 ez_setup.py</pre>
</div>
</blockquote>
<hr>
<h2>数据库连接</h2>
<p>连接数据库前,请先确认以下事项:</p>
<ul>
<li>您已经创建了数据库 TESTDB(测试数据库)</li>
<li>在TESTDB数据库中您已经创建了表 EMPLOYEE(测试用的测试表)</li>
<li>EMPLOYEE表字段为 FIRST_NAME, LAST_NAME, AGE, SEX 和 INCOME。</li>
<li>连接数据库TESTDB(测试数据库)使用的用户名为 "testuser" ,密码为 "test123",你可以可以自己设定或者直接使用root用户名及其密码,Mysql数据库用户授权请使用Grant命令。</li>
<li>在你的机子上已经安装了 Python MySQLdb 模块。</li>
<li>如果你对sql语句不熟悉,可以自己先简单的学习一下</li>
</ul>
<p>1、查看有没有创建数据库TESTDB(测试数据库),打开MySQL的client界面输入密码 后,输入命令:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> show databases;</pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522133027447-1253841502.png" alt=""></p>
<p>2、看到没有那个数据库,那我们就开始创建一个TESTDB数据库,输入命令:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> create database TESTDB;</pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522133659838-489646343.png" alt=""></p>
<p>3、再次输入第一步的命令查看,看到已经成功创建</p>
<p>&nbsp;<img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522133728435-496568491.png" alt=""></p>
<p>4、查看TESTDB数据库里,有没有EMPLOYEE(测试用的测试表),输入命令:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">use testdb;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> show tables;</pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522134117555-151928111.png" alt=""></p>
<p>5、查看到在TESTDB数据库中没有EMPLOYEE表,那么没有我们就创建一张EMPLOYEE表即可。</p>
<h2>创建一个实例</h2>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522135623241-766602808.png" alt=""></p>
<p>运行结果:(从结果中我们可以看见成功创建了一个Connection和Cursor对象。)</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522135652161-1858329714.png" alt=""></p>
<h2>下面将以具体代码的形式依次介绍python中如何实现对MySQL数据库的增删改查等操作。</h2>
<h3>实例:</h3>
<h4>1、连接数据库</h4>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522140536120-1399975454.png" alt=""></p>
<p>运行结果:</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522140638667-843910318.png" alt=""></p>
<p>参考代码:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> # coding=utf-<span style="color: rgba(128, 0, 128, 1)">8</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> # <span style="color: rgba(128, 0, 128, 1)">1</span>.先设置编码,utf-<span style="color: rgba(0, 0, 0, 1)">8可支持中英文,如上,一般放在第一行
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> # <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">.注释:包括记录创建时间,创建人,项目名称。
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(128, 0, 0, 1)">'''
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> Created on <span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">5</span>-<span style="color: rgba(128, 0, 128, 1)">22</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> @author: 北京-<span style="color: rgba(0, 0, 0, 1)">宏哥
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">Project:学习和使用python操作MySQL数据库
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(128, 0, 0, 1)">'''
</span><span style="color: rgba(0, 128, 128, 1)">10</span> # <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">.导入模块
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">import pymysql
</span><span style="color: rgba(0, 128, 128, 1)">12</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">import pymysql
</span><span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)"># 打开数据库连接
</span><span style="color: rgba(0, 128, 128, 1)">16</span> conn = pymysql.connect(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">testdb</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">17</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)"># 使用cursor()方法创建一个游标对象
</span><span style="color: rgba(0, 128, 128, 1)">19</span> cursor =<span style="color: rgba(0, 0, 0, 1)"> conn.cursor()
</span><span style="color: rgba(0, 128, 128, 1)">20</span>
<span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 0, 1)"># 使用execute()方法执行SQL查询
</span><span style="color: rgba(0, 128, 128, 1)">22</span> cursor.execute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">SELECT VERSION()</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">23</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 0, 1)"># 使用fetchone()方法获取单条数据
</span><span style="color: rgba(0, 128, 128, 1)">25</span> data =<span style="color: rgba(0, 0, 0, 1)"> cursor.fetchone()
</span><span style="color: rgba(0, 128, 128, 1)">26</span>
<span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 0, 0, 1)"># 打印
</span><span style="color: rgba(0, 128, 128, 1)">28</span> print(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">database version: %s</span><span style="color: rgba(128, 0, 0, 1)">'</span> %<span style="color: rgba(0, 0, 0, 1)"> data)
</span><span style="color: rgba(0, 128, 128, 1)">29</span>
<span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 0, 0, 1)"># 关闭数据库连接
</span><span style="color: rgba(0, 128, 128, 1)">31</span> conn.close()</pre>
</div>
<h4>2、创建数据库表</h4>
<p>如果数据库连接存在我们可以使用execute()方法来为数据库创建表,如下所示创建表EMPLOYEE:</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522140959322-930458326.png" alt=""></p>
<p>运行代码后查看有没有表创建,输入命令:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">use TESTDB;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> show tables;</pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522141802760-1972503197.png" alt=""></p>
<p>运行结果:</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522142104484-1886215381.png" alt=""></p>
<p>参考代码:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> # coding=utf-<span style="color: rgba(128, 0, 128, 1)">8</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> # <span style="color: rgba(128, 0, 128, 1)">1</span>.先设置编码,utf-<span style="color: rgba(0, 0, 0, 1)">8可支持中英文,如上,一般放在第一行
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> # <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">.注释:包括记录创建时间,创建人,项目名称。
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(128, 0, 0, 1)">'''
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> Created on <span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">5</span>-<span style="color: rgba(128, 0, 128, 1)">22</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> @author: 北京-<span style="color: rgba(0, 0, 0, 1)">宏哥
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">Project:学习和使用python操作MySQL数据库
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(128, 0, 0, 1)">'''
</span><span style="color: rgba(0, 128, 128, 1)">10</span> # <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">.导入模块
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">import pymysql
</span><span style="color: rgba(0, 128, 128, 1)">12</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">import pymysql
</span><span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)"># 打开数据库连接
</span><span style="color: rgba(0, 128, 128, 1)">16</span> conn = pymysql.connect(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">testdb</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">17</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)"># 使用cursor()方法创建一个游标对象cursor
</span><span style="color: rgba(0, 128, 128, 1)">19</span> cursor =<span style="color: rgba(0, 0, 0, 1)"> conn.cursor()# 游标对象用于执行查询和获取结果
</span><span style="color: rgba(0, 128, 128, 1)">20</span>
<span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 0, 1)"># 使用execute()方法执行SQL,如果表存在则将其删除
</span><span style="color: rgba(0, 128, 128, 1)">22</span> cursor.execute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">DROP TABLE IF EXISTS EMPLOYEE</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">23</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 0, 1)"># 使用预处理语句创建表
</span><span style="color: rgba(0, 128, 128, 1)">25</span> sql = <span style="color: rgba(128, 0, 0, 1)">"""</span><span style="color: rgba(128, 0, 0, 1)">CREATE TABLE `employee` (</span>
<span style="color: rgba(0, 128, 128, 1)">26</span>   `first_name` varchar(<span style="color: rgba(128, 0, 128, 1)">255</span>) DEFAULT NULL COMMENT <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">姓</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">27</span>   `last_name` varchar(<span style="color: rgba(128, 0, 128, 1)">255</span>) DEFAULT NULL COMMENT <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">名</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">28</span>   `age` <span style="color: rgba(0, 0, 255, 1)">int</span>(<span style="color: rgba(128, 0, 128, 1)">11</span>) DEFAULT NULL COMMENT <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">年龄</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">29</span>   `sex` varchar(<span style="color: rgba(128, 0, 128, 1)">255</span>) DEFAULT NULL COMMENT <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">性别</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">30</span>   `income` varchar(<span style="color: rgba(128, 0, 128, 1)">255</span>) DEFAULT NULL COMMENT <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">收入</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">31</span> ) ENGINE=InnoDB DEFAULT CHARSET=<span style="color: rgba(0, 0, 0, 1)">utf8;
</span><span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(128, 0, 0, 1)">"""
</span><span style="color: rgba(0, 128, 128, 1)">33</span>
<span style="color: rgba(0, 128, 128, 1)">34</span> <span style="color: rgba(0, 0, 0, 1)"># 执行SQL语句
</span><span style="color: rgba(0, 128, 128, 1)">35</span> <span style="color: rgba(0, 0, 0, 1)">cursor.execute(sql)
</span><span style="color: rgba(0, 128, 128, 1)">36</span>
<span style="color: rgba(0, 128, 128, 1)">37</span> <span style="color: rgba(0, 0, 0, 1)"># 关闭数据库连接
</span><span style="color: rgba(0, 128, 128, 1)">38</span> conn.close()</pre>
</div>
<h4>3、数据库插入操作</h4>
<p>以下实例使用执行 SQL INSERT 语句向表 EMPLOYEE 插入记录:</p>
<p>1、首先查询有没有记录输入查询语句:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">select</span> * <span style="color: rgba(0, 0, 255, 1)">from</span> employee;</pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522142734620-715136586.png" alt=""></p>
<p>2、代码实现</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522143028200-1161510174.png" alt=""></p>
<p>运行结果:</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522143058549-976392202.png" alt=""></p>
<p>再次查看有没有记录,输入第一步的sql语句:(可以看到插入一条记录)</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522143215144-1434316013.png" alt=""></p>
<p>参考代码:</p>
<div class="cnblogs_code">
<pre># coding=utf-<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">
# </span><span style="color: rgba(128, 0, 128, 1)">1</span>.先设置编码,utf-<span style="color: rgba(0, 0, 0, 1)">8可支持中英文,如上,一般放在第一行

# </span><span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">.注释:包括记录创建时间,创建人,项目名称。
</span><span style="color: rgba(128, 0, 0, 1)">'''
</span>Created on <span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">5</span>-<span style="color: rgba(128, 0, 128, 1)">22</span><span style="color: rgba(0, 0, 0, 1)">
@author: 北京</span>-<span style="color: rgba(0, 0, 0, 1)">宏哥
Project:学习和使用python操作MySQL数据库
</span><span style="color: rgba(128, 0, 0, 1)">'''
</span># <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">.导入模块
import pymysql

import pymysql

# 打开数据库连接
conn </span>= pymysql.connect(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">testdb</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

# 使用cursor()方法获取操作游标
cursor </span>=<span style="color: rgba(0, 0, 0, 1)"> conn.cursor()

# SQL语句:向数据表中插入数据
sql </span>= <span style="color: rgba(128, 0, 0, 1)">"""</span><span style="color: rgba(128, 0, 0, 1)">INSERT INTO EMPLOYEE(FIRST_NAME,</span>
<span style="color: rgba(0, 0, 0, 1)">         LAST_NAME, AGE, SEX, INCOME)
         VALUES (</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Mac</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Mohan</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 128, 1)">20</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">M</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 128, 1)">2000</span>)<span style="color: rgba(128, 0, 0, 1)">"""
</span><span style="color: rgba(0, 0, 0, 1)">
# 异常处理
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
    # 执行SQL语句
    cursor.execute(sql)
    # 提交事务到数据库执行
    conn.commit()# 事务是访问和更新数据库的一个程序执行单元
except:
    # 如果发生错误则执行回滚操作
    conn.rollback()

# 关闭数据库连接
conn.close()</span></pre>
</div>
<p>另一种写法:</p>
<div class="cnblogs_code">
<pre># coding=utf-<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">
# </span><span style="color: rgba(128, 0, 128, 1)">1</span>.先设置编码,utf-<span style="color: rgba(0, 0, 0, 1)">8可支持中英文,如上,一般放在第一行

# </span><span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">.注释:包括记录创建时间,创建人,项目名称。
</span><span style="color: rgba(128, 0, 0, 1)">'''
</span>Created on <span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">5</span>-<span style="color: rgba(128, 0, 128, 1)">22</span><span style="color: rgba(0, 0, 0, 1)">
@author: 北京</span>-<span style="color: rgba(0, 0, 0, 1)">宏哥
Project:学习和使用python操作MySQL数据库
</span><span style="color: rgba(128, 0, 0, 1)">'''
</span># <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">.导入模块
import pymysql

import pymysql

# 打开数据库连接
conn </span>= pymysql.connect(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">testdb</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

# 使用cursor()方法获取操作游标
cursor </span>=<span style="color: rgba(0, 0, 0, 1)"> conn.cursor()

# SQL语句:向数据表中插入数据
sql </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">INSERT INTO EMPLOYEE(FIRST_NAME, \</span>
<span style="color: rgba(0, 0, 0, 1)">       LAST_NAME, AGE, SEX, INCOME) \
       VALUES (</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">%s</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">%s</span><span style="color: rgba(128, 0, 0, 1)">'</span>,%s,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">%s</span><span style="color: rgba(128, 0, 0, 1)">'</span>,%s)<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"> % \</span>
       (<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Mac</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Mohan</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 128, 1)">20</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">M</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 128, 1)">2000</span><span style="color: rgba(0, 0, 0, 1)">)
# 异常处理
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
    # 执行SQL语句
    cursor.execute(sql)
    # 提交事务到数据库执行
    conn.commit()# 事务是访问和更新数据库的一个程序执行单元
except:
    # 如果发生错误则执行回滚操作
    conn.rollback()

# 关闭数据库连接
conn.close()</span></pre>
</div>
<h3>实例:</h3>
<p>以下代码使用变量向SQL语句中传递参数:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">..................................
</span><span style="color: rgba(0, 128, 128, 1)">2</span> user_id = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> password = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 128, 1)">4</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> con.execute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">insert into Login values( %s,%s)</span><span style="color: rgba(128, 0, 0, 1)">'</span> %<span style="color: rgba(0, 0, 0, 1)"> \
</span><span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 0, 0, 1)">             (user_id, password))
</span><span style="color: rgba(0, 128, 128, 1)">7</span> ..................................</pre>
</div>
<h4>3、数据库查询操作</h4>
<p>Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。</p>
<ul>
<li><strong>fetchone():</strong>&nbsp;该方法获取下一个查询结果集。结果集是一个对象</li>
<li><strong>fetchall():</strong>接收全部的返回结果行.</li>
<li><strong>rowcount:</strong>&nbsp;这是一个只读属性,并返回执行execute()方法后影响的行数。</li>
</ul>
<h3>实例:</h3>
<p>查询EMPLOYEE表中salary(工资)字段大于1000的所有数据:</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522143846155-1580202610.png" alt=""></p>
<p>运行结果:</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522143913198-328248229.png" alt=""></p>
<p>参考代码:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> # coding=utf-<span style="color: rgba(128, 0, 128, 1)">8</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> # <span style="color: rgba(128, 0, 128, 1)">1</span>.先设置编码,utf-<span style="color: rgba(0, 0, 0, 1)">8可支持中英文,如上,一般放在第一行
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> # <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">.注释:包括记录创建时间,创建人,项目名称。
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(128, 0, 0, 1)">'''
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> Created on <span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">5</span>-<span style="color: rgba(128, 0, 128, 1)">22</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> @author: 北京-<span style="color: rgba(0, 0, 0, 1)">宏哥
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">Project:学习和使用python操作MySQL数据库
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(128, 0, 0, 1)">'''
</span><span style="color: rgba(0, 128, 128, 1)">10</span> # <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">.导入模块
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">import pymysql
</span><span style="color: rgba(0, 128, 128, 1)">12</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">import pymysql
</span><span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)"># 打开数据库连接
</span><span style="color: rgba(0, 128, 128, 1)">16</span> conn = pymysql.connect(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">testdb</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">17</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)"># 使用cursor()方法获取操作游标
</span><span style="color: rgba(0, 128, 128, 1)">19</span> cursor =<span style="color: rgba(0, 0, 0, 1)"> conn.cursor()
</span><span style="color: rgba(0, 128, 128, 1)">20</span>
<span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 0, 1)"># SQL语句:查询
</span><span style="color: rgba(0, 128, 128, 1)">22</span> sql = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">SELECT * FROM employee WHERE income &gt; 1000 </span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 128, 1)">23</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 0, 1)"># 异常处理
</span><span style="color: rgba(0, 128, 128, 1)">25</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 0, 1)">    # 执行SQL语句
</span><span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 0, 0, 1)">    cursor.execute(sql)
</span><span style="color: rgba(0, 128, 128, 1)">28</span> <span style="color: rgba(0, 0, 0, 1)">    # 获取所有的记录列表
</span><span style="color: rgba(0, 128, 128, 1)">29</span>   results =<span style="color: rgba(0, 0, 0, 1)"> cursor.fetchall()
</span><span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 0, 0, 1)">    # 遍历列表
</span><span style="color: rgba(0, 128, 128, 1)">31</span>   <span style="color: rgba(0, 0, 255, 1)">for</span> row <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> results:
</span><span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 0, 0, 1)">      # 打印列表元素
</span><span style="color: rgba(0, 128, 128, 1)">33</span> <span style="color: rgba(0, 0, 0, 1)">      print(row)
</span><span style="color: rgba(0, 128, 128, 1)">34</span> <span style="color: rgba(0, 0, 0, 1)">      # 姓
</span><span style="color: rgba(0, 128, 128, 1)">35</span>         first_name = row[<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">]
</span><span style="color: rgba(0, 128, 128, 1)">36</span> <span style="color: rgba(0, 0, 0, 1)">      # 名
</span><span style="color: rgba(0, 128, 128, 1)">37</span>         last_name = row[<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">]
</span><span style="color: rgba(0, 128, 128, 1)">38</span> <span style="color: rgba(0, 0, 0, 1)">      # 年龄
</span><span style="color: rgba(0, 128, 128, 1)">39</span>         age = row[<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">]
</span><span style="color: rgba(0, 128, 128, 1)">40</span> <span style="color: rgba(0, 0, 0, 1)">      # 性别
</span><span style="color: rgba(0, 128, 128, 1)">41</span>         sex = row[<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">]
</span><span style="color: rgba(0, 128, 128, 1)">42</span> <span style="color: rgba(0, 0, 0, 1)">      # 收入
</span><span style="color: rgba(0, 128, 128, 1)">43</span>         income = row[<span style="color: rgba(128, 0, 128, 1)">4</span><span style="color: rgba(0, 0, 0, 1)">]
</span><span style="color: rgba(0, 128, 128, 1)">44</span> <span style="color: rgba(0, 0, 0, 1)">      # 打印列表元素
</span><span style="color: rgba(0, 128, 128, 1)">45</span> <span style="color: rgba(0, 0, 0, 1)">      print(first_name, last_name, age, sex, income)
</span><span style="color: rgba(0, 128, 128, 1)">46</span> <span style="color: rgba(0, 0, 0, 1)">except:
</span><span style="color: rgba(0, 128, 128, 1)">47</span>   print(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Uable to fetch data!</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">48</span>
<span style="color: rgba(0, 128, 128, 1)">49</span> <span style="color: rgba(0, 0, 0, 1)"># 关闭数据库连接
</span><span style="color: rgba(0, 128, 128, 1)">50</span> conn.close()</pre>
</div>
<h4>4、数据库更新操作</h4>
<p>更新操作用于更新数据表的的数据,以下实例将 EMPLOYEE 表中的 SEX 字段为 'M' 的 AGE 字段递增 5:</p>
<p>1、以前age是20,</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522144249346-485014602.png" alt=""></p>
<p>2、代码实现:</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522144451993-596997079.png" alt=""></p>
<p>3、运行结果:</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522144512041-1863872617.png" alt=""></p>
<p>4、查询age变更到25了没有</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522144603004-1034685737.png" alt=""></p>
<p>5、参考代码:</p>
<div class="cnblogs_code">
<pre># coding=utf-<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">
# </span><span style="color: rgba(128, 0, 128, 1)">1</span>.先设置编码,utf-<span style="color: rgba(0, 0, 0, 1)">8可支持中英文,如上,一般放在第一行

# </span><span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">.注释:包括记录创建时间,创建人,项目名称。
</span><span style="color: rgba(128, 0, 0, 1)">'''
</span>Created on <span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">5</span>-<span style="color: rgba(128, 0, 128, 1)">22</span><span style="color: rgba(0, 0, 0, 1)">
@author: 北京</span>-<span style="color: rgba(0, 0, 0, 1)">宏哥
Project:学习和使用python操作MySQL数据库
</span><span style="color: rgba(128, 0, 0, 1)">'''
</span># <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">.导入模块
import pymysql

import pymysql

# 打开数据库连接
conn </span>= pymysql.connect(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">testdb</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

# 使用cursor()方法获取操作游标
cursor </span>=<span style="color: rgba(0, 0, 0, 1)"> conn.cursor()

# SQL语句,执行更新操作
sql </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">UPDATE employee SET age = age + 5 WHERE sex = "M"</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">

# 异常处理
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
    # 执行SQL语句
    cursor.execute(sql)
    # 提交到数据库执行
    conn.commit()
except:
    # 发生错误时回滚
    conn.rollback()

# 关闭数据库连接
conn.close()</span></pre>
</div>
<h4>5、删除操作</h4>
<p>删除操作用于删除数据表中的数据,以下实例演示了删除数据表 EMPLOYEE 中 AGE 大于 20 的所有数据:</p>
<p>1、大于20的只有一条,删除了就没有记录了,输入</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span><span style="color: rgba(0, 0, 255, 1)">select</span> * <span style="color: rgba(0, 0, 255, 1)">from</span> employee;</pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522145115234-2051097733.png" alt=""></p>
<p>2、代码实现</p>
<p>&nbsp;<img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522145318688-692528367.png" alt=""></p>
<p>3、运行结果</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522145305518-1938594856.png" alt=""></p>
<p>4、查看数据库表,重复第一步</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190522152659620-917907207.png" alt=""></p>
<p>&nbsp;</p>
<p>5、参考代码</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> # coding=utf-<span style="color: rgba(128, 0, 128, 1)">8</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> # <span style="color: rgba(128, 0, 128, 1)">1</span>.先设置编码,utf-<span style="color: rgba(0, 0, 0, 1)">8可支持中英文,如上,一般放在第一行
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> # <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">.注释:包括记录创建时间,创建人,项目名称。
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(128, 0, 0, 1)">'''
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> Created on <span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">5</span>-<span style="color: rgba(128, 0, 128, 1)">22</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> @author: 北京-<span style="color: rgba(0, 0, 0, 1)">宏哥
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">Project:学习和使用python操作MySQL数据库
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(128, 0, 0, 1)">'''
</span><span style="color: rgba(0, 128, 128, 1)">10</span> # <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">.导入模块
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">import pymysql
</span><span style="color: rgba(0, 128, 128, 1)">12</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">import pymysql
</span><span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)"># 打开数据库连接
</span><span style="color: rgba(0, 128, 128, 1)">16</span> conn = pymysql.connect(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">testdb</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">17</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)"># 使用cursor()方法获取操作游标
</span><span style="color: rgba(0, 128, 128, 1)">19</span> cursor =<span style="color: rgba(0, 0, 0, 1)"> conn.cursor()
</span><span style="color: rgba(0, 128, 128, 1)">20</span>
<span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 0, 1)"># SQL语句,执行删除操作
</span><span style="color: rgba(0, 128, 128, 1)">22</span> sql = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">DELETE FROM employee WHERE age &gt;20</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">23</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 0, 1)"># 异常处理
</span><span style="color: rgba(0, 128, 128, 1)">25</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 0, 1)">    # 执行SQL语句
</span><span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 0, 0, 1)">    cursor.execute(sql)
</span><span style="color: rgba(0, 128, 128, 1)">28</span> <span style="color: rgba(0, 0, 0, 1)">    # 提交到数据库执行
</span><span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 0, 0, 1)">    conn.commit()
</span><span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 0, 0, 1)">except:
</span><span style="color: rgba(0, 128, 128, 1)">31</span> <span style="color: rgba(0, 0, 0, 1)">    # 发生错误时回滚
</span><span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 0, 0, 1)">    conn.rollback()
</span><span style="color: rgba(0, 128, 128, 1)">33</span>
<span style="color: rgba(0, 128, 128, 1)">34</span> <span style="color: rgba(0, 0, 0, 1)"># 关闭数据库连接
</span><span style="color: rgba(0, 128, 128, 1)">35</span> conn.close()</pre>
</div>
<h2>执行事务</h2>
<p>事务机制可以确保数据一致性。</p>
<p>事务应该具有4个属性:原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性。</p>
<ul>
<li>原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。</li>
<li>一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。</li>
<li>隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。</li>
<li>持久性(durability)。持续性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。</li>
</ul>
<p>Python DB API 2.0 的事务提供了两个方法 commit 或 rollback。</p>
<h3>实例:</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)"># SQL删除记录语句
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> sql = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">DELETE FROM EMPLOYEE WHERE AGE &gt; %s</span><span style="color: rgba(128, 0, 0, 1)">"</span> % (<span style="color: rgba(128, 0, 128, 1)">20</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 0, 1)">   # 执行SQL语句
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)">   cursor.execute(sql)
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)">   # 向数据库提交
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)">   db.commit()
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">except:
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 0, 1)">   # 发生错误时回滚
</span><span style="color: rgba(0, 128, 128, 1)">10</span>    db.rollback()</pre>
</div>
<p>对于支持事务的数据库, 在Python数据库编程中,当游标建立之时,就自动开始了一个隐形的数据库事务。</p>
<p>commit()方法游标的所有更新操作,rollback()方法回滚当前游标的所有操作。每一个方法都开始了一个新的事务。</p>
<hr>
<h2>错误处理</h2>
<p>DB API中定义了一些数据库操作的错误及异常,下表列出了这些错误和异常:</p>
<table class="reference">
<tbody>
<tr><th>异常</th><th>描述</th></tr>
<tr>
<td>Warning</td>
<td>当有严重警告时触发,例如插入数据是被截断等等。必须是 StandardError 的子类。</td>
</tr>
<tr>
<td>Error</td>
<td>警告以外所有其他错误类。必须是 StandardError 的子类。</td>
</tr>
<tr>
<td>InterfaceError</td>
<td>当有数据库接口模块本身的错误(而不是数据库的错误)发生时触发。 必须是Error的子类。</td>
</tr>
<tr>
<td>DatabaseError</td>
<td>和数据库有关的错误发生时触发。 必须是Error的子类。</td>
</tr>
<tr>
<td>DataError</td>
<td>当有数据处理时的错误发生时触发,例如:除零错误,数据超范围等等。 必须是DatabaseError的子类。</td>
</tr>
<tr>
<td>OperationalError</td>
<td>指非用户控制的,而是操作数据库时发生的错误。例如:连接意外断开、 数据库名未找到、事务处理失败、内存分配错误等等操作数据库是发生的错误。 必须是DatabaseError的子类。</td>
</tr>
<tr>
<td>IntegrityError</td>
<td>完整性相关的错误,例如外键检查失败等。必须是DatabaseError子类。</td>
</tr>
<tr>
<td>InternalError</td>
<td>数据库的内部错误,例如游标(cursor)失效了、事务同步失败等等。 必须是DatabaseError子类。</td>
</tr>
<tr>
<td>ProgrammingError</td>
<td>程序错误,例如数据表(table)没找到或已存在、SQL语句语法错误、 参数数量错误等等。必须是DatabaseError的子类。</td>
</tr>
<tr>
<td>NotSupportedError</td>
<td>不支持错误,指使用了数据库不支持的函数或API等。例如在连接对象上 使用.rollback()函数,然而数据库并不支持事务或者事务已关闭。 必须是DatabaseError的子类。</td>
</tr>
</tbody>
</table>
<h2>小结</h2>
<p>&nbsp;  哈哈,终于搞完了、整完了,累死宝宝了,已到半夜,明天有时间分享!!!</p>
<p>  小伙伴python操作MySQL数据库就是这么简单!!!最重要的自己要会、要有扎实的SQL基础。</p>

</div>
<div id="MySignature" role="contentinfo">
    <div id="MySignature" style="display: block">
        <div style="font-size: 13px; border: 1px dashed rgb(45, 161, 45); padding: 10px 15px; background-color: rgb(248, 248, 248)">
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家在移动端也能看到我分享的博文,现已注册个人微信公众号,扫描左下方二维码即可,欢迎大家关注,提前解锁更多测试干货!有时间会及时分享相关技术博文。
                </label>
                <br>
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家互动讨论相关技术问题,刚刚建立了咱们的专门的微信群交流互动群,群内会分享交流测试领域前沿知识。请您扫描中间的微信二维码进群
                </label>
                <br>
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家互动讨论相关技术问题,现已组建专门的微信群,由于微信群满100,请您扫描右下方宏哥个人微信二维码拉你进群
                        <label style="font-weight: bold; color: red; font-size: 15px">
                                (请务必备注:已关注公众号进群)平时上班忙(和你一样),所以加好友不及时,请稍安勿躁~
                        </label>
                        ,欢迎大家加入这个大家庭,我们一起畅游知识的海洋。
                </label>
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;感谢您花时间阅读此篇文章,如果您觉得这篇文章你学到了东西也是为了犒劳下博主的码字不易不妨打赏一下吧,让博主能喝上一杯咖啡,在此谢过了!
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;如果您觉得阅读本文对您有帮助,请点一下左下角
               
                        “推荐”
               
                按钮,您的
                <label style="font-weight: bold; color: red; font-size: 15px">
                        “推荐”
                </label>
                将是我最大的写作动力!另外您也可以选择
               
                        【
                        <strong>
                                关注我
                        </strong>
                        】
               
                ,可以很方便找到我!
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;本文版权归作者和博客园共有,来源网址:
               
                        https://www.cnblogs.com/du-hong
               
                欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利!
        </div>
        <div style="text-align: center; margin-top: 10px">
                <p style=" font-weight: bolder; color: red; ">
                        公众号(关注宏哥)&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace; &NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        微信群(扫码进群) &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;客服微信
                </p>
                <img style="width: 200px;padding-right: 50px;" alt="个人微信公众号" src="https://img2018.cnblogs.com/common/1741949/201911/1741949-20191119095948011-608816619.png">
                <img style="width: 200px;padding-right: 65px;" alt="微信群" src="https://img2024.cnblogs.com/blog/1232840/202506/1232840-20250610113707419-637869921.png">
                <img style="width: 200px" alt="个人微信" src="https://img2018.cnblogs.com/common/1741949/201911/1741949-20191106101257091-849954564.png">
        </div>
</div><br><br>
来源:https://www.cnblogs.com/du-hong/p/10897822.html
頁: [1]
查看完整版本: python接口自动化(三十八)-python操作mysql数据库(详解)