Python-Operation_MySQL
<h2 style="color: black; text-align: left; margin-bottom: 10px;">操作流程</h2>安装 MySQL 数据库pymysql 模块安装与<span style="color: black;">运用</span>获取数据库的连接执行 sql 语句或存储过程关闭数据库连接<h3 style="color: black; text-align: left; margin-bottom: 10px;">安装 MySQL 数据库</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">重视</span>:</strong> 安装过程<span style="color: black;">咱们</span><span style="color: black;">必须</span><span style="color: black;">经过</span>开启管理员权限来安装,否则会<span style="color: black;">因为</span>权限不足<span style="color: black;">引起</span><span style="color: black;">没法</span>安装。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1、</span>Linux 上安装 MySQL</h3>1、检测系统<span style="color: black;">是不是</span>自带安装 MySQL: <span style="color: black;">rpm</span> -qa | grep mysql
2、<span style="color: black;">倘若</span>查看到系统有安装 MySQL,那<span style="color: black;">能够</span><span style="color: black;">选取</span>进行卸载:<span style="color: black;">rpm</span> -e mysql… <span style="color: black;"># 普通删除模式</span>
rpm -e --nodeps mysql… <span style="color: black;"># 强力删除模式,<span style="color: black;">倘若</span><span style="color: black;">运用</span>上面命令删除时,提示有依赖的其它文件,则用该命令<span style="color: black;">能够</span>对其进行强力删除</span>3、安装 MySQL<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span><span style="color: black;">必须</span>先下载 MySQL 安装包,官网下载<span style="color: black;">位置</span> 下载对应版本<span style="color: black;">就可</span>,或直接在网上拉取并安装:</p>wget http://repo.mysql.com/mysql-community-<span style="color: black;">release</span>-el7<span style="color: black;">-5.</span>noarch.rpm <span style="color: black;"># 获取安装包</span>
rpm -ivh mysql-community-<span style="color: black;">release</span>-el7<span style="color: black;">-5.</span>noarch.rpm <span style="color: black;"># 安装</span>
yum <span style="color: black;">update</span>
yum <span style="color: black;">install</span> mysql-<span style="color: black;">server</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">权限设置:</strong></p>chown mysql:mysql -R /<span style="color: black;">var</span>/lib/mysql
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">初始化 MySQL:</strong></p>mysqld <span style="color: black;">--initialize</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">起步</span> MySQL:</strong></p><span style="color: black;">systemctl</span>start mysqld<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">查看 MySQL 运行状态:</strong></p>systemctl <span style="color: black;">status</span> mysqld
4、修改默认<span style="color: black;">秘码</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Mysql安装成功后,默认的root用户<span style="color: black;">秘码</span>为空,你<span style="color: black;">能够</span><span style="color: black;">运用</span>以下命令来创建root用户的<span style="color: black;">秘码</span>:</p><span style="color: black;">mysqladmin</span> -u root password <span style="color: black;">"new_password"</span>;
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">登陆:</strong></p><span style="color: black;">mysql</span>-uroot -p<span style="color: black;">new_password</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2、</span>Windows 上安装</h3>1、下载<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Windows 上安装 MySQL 相对<span style="color: black;">来讲</span>会较为简单,5.7 版本下载<span style="color: black;">位置</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下载完后,将 zip 包解压到自定义目录,<span style="color: black;">这儿</span>我将解压后的文件夹放在 D:\Software\MySQL\mysql-5.7.28 下;</p>2、配置<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">接下来<span style="color: black;">必须</span>配置下 MySQL 的配置文件,打开<span style="color: black;">刚才</span>解压的文件夹 D:\Software\MySQL\mysql-5.7.28 ,在该文件夹下创建 my.ini 配置文件,编辑 my.ini 配置以下基本信息:</p><span style="color: black;"></span>
<span style="color: black;"># 设置mysql客户端默认字符集</span>
<span style="color: black;">default-character-set</span>=utf8
<span style="color: black;"></span>
<span style="color: black;"># 设置3306端口</span>
<span style="color: black;">port</span> = <span style="color: black;">3306</span>
<span style="color: black;"># 设置mysql的安装目录</span>
<span style="color: black;">basedir</span>=D:\\Software\\MySQL\\mysql-<span style="color: black;">5.7</span>.<span style="color: black;">28</span>
<span style="color: black;"># 设置 mysql数据库的数据的存放目录,MySQL 8+ 不<span style="color: black;">必须</span>以下配置,系统自己生成<span style="color: black;">就可</span>,否则有可能报错</span>
<span style="color: black;">datadir</span>=D:\\Software\\MySQL\\SqlData<span style="color: black;"># <span style="color: black;">准许</span>最大连接数</span>
<span style="color: black;">max_connections</span>=<span style="color: black;">20</span>
<span style="color: black;"># 服务端<span style="color: black;">运用</span>的字符集默认为8比特编码的latin1字符集</span>
<span style="color: black;">character-set-server</span>=utf8
<span style="color: black;"># 创建新表时将<span style="color: black;">运用</span>的默认存储引擎</span>
<span style="color: black;">default-storage-engine</span>=INNODB
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">注意</strong>:还<span style="color: black;">必须</span>创建数据库数据目录,初始化数据库的时候<span style="color: black;">必须</span>初始化数据目录 在 Mysql 安装目录下创建数据目录 datadir=D:\\Software\\MySQL\\SqlData</p>3、<span style="color: black;">起步</span>数据库<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以管理员身份打开 cmd 命令行工具,切换至 Mysql 安装目录:</p><span style="color: black;">cd</span> <span style="color: black;">D</span>:\<span style="color: black;">Software</span>\<span style="color: black;">MySQL</span>\<span style="color: black;">mysql-5</span><span style="color: black;">.7</span><span style="color: black;">.28</span>\<span style="color: black;">bin</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">初始化数据库:</strong></p>mysqld --initialize --<span style="color: black;">console</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">执行完成后,会输出 root 用户的初始默认<span style="color: black;">秘码</span>,如下所示:</p>A temporary password <span style="color: black;">is</span> generated <span style="color: black;">for</span> root@localhost: feKm1E/-ExWM
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">“feKm1E/-ExWM” 即为 Mysql 初始<span style="color: black;">秘码</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">输入以下安装命令:</strong></p><span style="color: black;">mysqld</span> install
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">初始化数据目录</strong></p>mysqld <span style="color: black;">--initialize-insecure </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">起步</span> Mysql:</strong></p><span style="color: black;">net</span> start mysql
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">登陆:</strong></p><span style="color: black;">mysql</span> -uroot -p
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">刚安装好的数据库 默认<span style="color: black;">秘码</span>是空,可<span style="color: black;">按照</span><span style="color: black;">自己</span><span style="color: black;">必须</span>修改。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">windows 下修改 mysql <span style="color: black;">秘码</span>:</strong></p><span style="color: black;">mysql></span><span style="color: black;"> select User from user;</span>+---------------+
| User |
+---------------+
| mysql.session |
| mysql.sys |
| root |
+---------------+
3 rows in set (0.00 sec)<span style="color: black;">
mysql></span>
<span style="color: black;">mysql></span><span style="color: black;"> update mysql.user <span style="color: black;">set</span> authentication_string=password(<span style="color: black;">123456</span>) <span style="color: black;">where</span> user=<span style="color: black;">root</span>;</span>Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1<span style="color: black;">
mysql></span><span style="color: black;"> flush privileges; <span style="color: black;"># 立即生效</span></span>
Query OK, 0 rows affected (0.01 sec)
<span style="color: black;">
mysql></span><span style="color: black;"> quit</span>
<span style="color: black;">#</span><span style="color: black;"> 退出重新登陆<span style="color: black;">就可</span></span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3、</span>创建数据库</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">创建数据库:</strong></p><span style="color: black;">create</span> <span style="color: black;">database</span> test_db;
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">查看数据库:</strong></p>mysql> show databases;
+--------------------+
<span style="color: black;">| Database |</span>
+--------------------+
<span style="color: black;">| information_schema |</span>
<span style="color: black;">| mysql |</span>
<span style="color: black;">| performance_schema |</span>
<span style="color: black;">| sys |</span>
<span style="color: black;">| test_db |</span>
+--------------------+
<span style="color: black;">5</span> rows <span style="color: black;">in</span> set (<span style="color: black;">0</span>.<span style="color: black;">00</span> sec)
mysql> use test_db;
Database changed
<h2 style="color: black; text-align: left; margin-bottom: 10px;">pymysql 模块安装与数据 CURD</h2>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1、</span>安装</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PyMySQL 模块<span style="color: black;">运用</span> pip命令进行安装:</p><span style="color: black;">pip3</span> install PyMySQL
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">假如系统不支持 pip 命令,<span style="color: black;">能够</span><span style="color: black;">运用</span>以下方式安装:</p><span style="color: black;">$</span><span style="color: black;"> git <span style="color: black;">clone</span> https://github.com/PyMySQL/PyMySQL</span>
<span style="color: black;">$</span><span style="color: black;"> <span style="color: black;">cd</span> PyMySQL/</span>
<span style="color: black;">$</span><span style="color: black;">python3 setup.py install</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2、</span>pymysql 连接数据库</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">pymysql .connect 函数:连接上数据库</strong></p><span style="color: black;"># 导入模块</span>
import pymysql
<span style="color: black;"># 打开数据库连接</span>
conn = pymysql.connect(
host=<span style="color: black;">"127.0.0.1"</span>,
user=<span style="color: black;">"root"</span>,
password=<span style="color: black;">"123456"</span>,
database=<span style="color: black;">"test_db"</span>,
charset=<span style="color: black;">"utf8"</span>)
<span style="color: black;">print</span>(conn)
<span style="color: black;">print</span>(<span style="color: black;">type</span>(conn))
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出结果<span style="color: black;">表示</span>如下:表面数据库连接成功</p><pymysql.connections.Connection <span style="color: black;">object</span> at <span style="color: black;">0x00000000022A54A8</span>>
<<span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">pymysql</span>.<span style="color: black;">connections</span>.<span style="color: black;">Connection</span>></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">运用</span> pymysql 的 connect() <span style="color: black;">办法</span>连接数据库,connect 参数解释如下:</strong></p>host:MySQL服务的<span style="color: black;">位置</span>,若数据库在本地上,<span style="color: black;">运用</span> localhost <span style="color: black;">或</span>127.0.0.1。<span style="color: black;">倘若</span>在其它的服务器上,则写对应的 IP<span style="color: black;">位置</span>port:服务的端口号,默认为3306,不写则为默认值。user:登录数据库的用户名passwd:登录 MySQL 的<span style="color: black;">秘码</span>db:数据库名charset:设置为 utf8 编码,<span style="color: black;">处理</span>存汉字乱码问题<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">conn.cursor():获取游标</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>要操作数据库,光连接数据是<span style="color: black;">不足</span>的,咱们<span style="color: black;">必要</span>拿到操作数据库的游标,<span style="color: black;">才可</span>进行后续的操作,游标的<span style="color: black;">重点</span><span style="color: black;">功效</span>是用来接收数据库操作后的返回结果,<span style="color: black;">例如</span>读取数据、添加数据。<span style="color: black;">经过</span>获取到的数据库连接实例 conn 下的 cursor() <span style="color: black;">办法</span>来创建游标,实例如下:</p><span style="color: black;"># 导入模块</span>
<span style="color: black;">import</span> <span style="color: black;">pymysql</span>
<span style="color: black;">
# 打开数据库连接</span>
<span style="color: black;">conn</span> = <span style="color: black;">pymysql.connect(</span>
<span style="color: black;">host</span>=<span style="color: black;">"127.0.0.1",</span>
<span style="color: black;">user</span>=<span style="color: black;">"root",</span>
<span style="color: black;">password</span>=<span style="color: black;">"123456",</span>
<span style="color: black;">database</span>=<span style="color: black;">"test_db",</span>
<span style="color: black;">charset</span>=<span style="color: black;">"utf8")</span>
<span style="color: black;">
# print(conn)</span>
<span style="color: black;"># print(type(conn))</span>
<span style="color: black;"># 获取连接下的游标</span>
<span style="color: black;">cursor_test</span> = <span style="color: black;">conn.cursor()</span>
<span style="color: black;">print(cursor_test)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出结果为:</p><<span style="color: black;">pymysql</span><span style="color: black;">.cursors</span><span style="color: black;">.Cursor</span> <span style="color: black;">object</span> <span style="color: black;">at</span> 0<span style="color: black;">x000000000A544B00</span>>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">cursor 返回一个游标实例对象,其中<span style="color: black;">包括</span>了<span style="color: black;">非常多</span>操作数据的<span style="color: black;">办法</span>,如执行sql语句,sql 执行命令:execute()和executemany()</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">execute(query,args=None):</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">函数<span style="color: black;">功效</span>:执行单条的sql语句,执行成功后返回受影响的行数参数说明:query:要执行的sql语句,字符串类型args:可选的序列或映射,用于query的参数值。<span style="color: black;">倘若</span>args为序列,query中<span style="color: black;">必要</span><span style="color: black;">运用</span>%s做占位符;<span style="color: black;">倘若</span>args为映射,query中<span style="color: black;">必要</span><span style="color: black;">运用</span>%(key)s做占位符</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/c36c6b7f47854363a131d85c41abbc12~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723899495&x-signature=%2FHDzwuGHCkSzPWZmL6mMdFB7hB4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">executemany(query,args=None):</strong></p>函数<span style="color: black;">功效</span>:批量执行sql语句,<span style="color: black;">例如</span>批量<span style="color: black;">插进</span>数据,执行成功后返回受影响的行数参数说明:query:要执行的sql语句,字符串类型args:嵌套的序列或映射,用于query的参数值<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">其他游标对象如下表:</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">名<span style="color: black;">叫作</span>描述close()关闭游标,之后游标不可用fetchone()返回一条<span style="color: black;">查找</span>结果fetchall()返回所有<span style="color: black;">查找</span>结果fetchmany()返回size条<span style="color: black;">查找</span>结果nextset()移动到下一条结果scroll(value,mode=’relative’)移动游标到指定行,<span style="color: black;">倘若</span>mode=’relative’,则<span style="color: black;">暗示</span>从当前行移动value条,<span style="color: black;">倘若</span>mode=‘absolute’,则<span style="color: black;">暗示</span>从结果集的<span style="color: black;">第1</span>行移动value条</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">完整数据库连接操作实例如下:</strong></p><span style="color: black;"># 导入模块</span>
<span style="color: black;">import</span> pymysql
<span style="color: black;"># 打开数据库连接</span>
conn = pymysql.connect(
host=<span style="color: black;">"127.0.0.1"</span>,
user=<span style="color: black;">"root"</span>,
password=<span style="color: black;">"123456"</span>,
database=<span style="color: black;">"test_db"</span>,
charset=<span style="color: black;">"utf8"</span>)
<span style="color: black;"># print(conn)</span>
<span style="color: black;"># print(type(conn))</span>
<span style="color: black;"># 获取连接下的游标</span>cursor_test = conn.cursor()<span style="color: black;">print</span>(cursor_test)
<span style="color: black;"># <span style="color: black;">运用</span> execute() <span style="color: black;">办法</span>执行 SQL <span style="color: black;">查找</span>,<span style="color: black;">查找</span>数据库版本</span>
cursor_test.execute(<span style="color: black;">"SELECT VERSION()"</span>)
<span style="color: black;"># <span style="color: black;">运用</span> fetchone() <span style="color: black;">办法</span>返回一条数据.</span>
data = cursor_test.fetchone()
<span style="color: black;">print</span>(<span style="color: black;">"Database version : %s "</span> % data)
<span style="color: black;"># 关闭数据库连接</span>
conn.close()
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">以上结果输出为:</strong></p><span style="color: black;">Database version :</span> <span style="color: black;">5.7</span><span style="color: black;">.28</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3、</span>创建表</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">创建表代码如下:</p><span style="color: black;">import</span> pymysql
<span style="color: black;"># 打开数据库连接</span>conn = pymysql.connect(
host=<span style="color: black;">"127.0.0.1"</span>,
user=<span style="color: black;">"root"</span>,
password=<span style="color: black;">"123456"</span>,
database=<span style="color: black;">"test_db"</span>,
charset=<span style="color: black;">"utf8"</span>)
<span style="color: black;"># 获取连接下的游标</span>
cursor_test = conn.cursor()
<span style="color: black;"># <span style="color: black;">运用</span> execute() <span style="color: black;">办法</span>执行 SQL,<span style="color: black;">倘若</span>表存在则删除</span>
cursor_test.execute(<span style="color: black;">"DROP TABLE IF EXISTS EMPLOYEE"</span>)
<span style="color: black;"># <span style="color: black;">运用</span>预处理语句创建表</span>
sql = <span style="color: black;">"""CREATE TABLE user1 (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )"""</span>
cursor_test.execute(sql)
<span style="color: black;"># 关闭数据库连接</span>
conn.close()
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如下所示数据库表创建成功:</p>mysql> desc user1;
+------------+----------+------+-----+---------+-------+
<span style="color: black;">| Field |</span> Type <span style="color: black;">| Null |</span> Key <span style="color: black;">| Default |</span> Extra <span style="color: black;">|
+------------+----------+------+-----+---------+-------+
|</span> FIRST_NAME <span style="color: black;">| char(20) |</span> NO <span style="color: black;">| |</span> NULL <span style="color: black;">| |</span>
<span style="color: black;">| LAST_NAME |</span> char(<span style="color: black;">20</span>) <span style="color: black;">| YES |</span> <span style="color: black;">| NULL |</span> <span style="color: black;">|
|</span> AGE <span style="color: black;">| int(11) |</span> YES <span style="color: black;">| |</span>NULL<span style="color: black;">| |</span>
<span style="color: black;">| SEX |</span> char(<span style="color: black;">1</span>) <span style="color: black;">| YES |</span> <span style="color: black;">| NULL |</span> <span style="color: black;">|
|</span> INCOME <span style="color: black;">| float |</span> YES <span style="color: black;">| |</span> NULL <span style="color: black;">| |</span>
+------------+----------+------+-----+---------+-------+
<span style="color: black;">5</span> rows <span style="color: black;">in</span> set (<span style="color: black;">0</span>.<span style="color: black;">00</span> sec)
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3、</span>插入数据</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">插进</span>数据实现代码:</p><span style="color: black;">import</span> pymysql
<span style="color: black;"># 打开数据库连接</span>
conn = pymysql.connect(
host=<span style="color: black;">"127.0.0.1"</span>,
user=<span style="color: black;">"root"</span>,
password=<span style="color: black;">"123456"</span>,
database=<span style="color: black;">"test_db"</span>,
charset=<span style="color: black;">"utf8"</span>)
<span style="color: black;"># 获取连接下的游标</span>
cursor_test = conn.cursor()
<span style="color: black;"># <span style="color: black;">运用</span>预处理语句创建表</span>
sql = <span style="color: black;">"""INSERT INTO user1(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES (Fei, Fei, 20, M, 1000)"""</span>
<span style="color: black;">try</span>:
<span style="color: black;"># 执行sql语句</span>
cursor_test.execute(sql)
<span style="color: black;"># 提交到数据库执行</span>
conn.commit()
<span style="color: black;">except</span>:
<span style="color: black;"># <span style="color: black;">倘若</span><span style="color: black;">出现</span>错误则回滚</span>
conn.rollback()
<span style="color: black;"># 关闭数据库连接</span>
conn.close()
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">插进</span>数据结果:</strong></p>mysql> select * from user1;
+------------+-----------+------+------+--------+<span style="color: black;">| FIRST_NAME |</span> LAST_NAME <span style="color: black;">| AGE |</span> SEX <span style="color: black;">| INCOME |</span>+------------+-----------+------+------+--------+<span style="color: black;">| Fei |</span> Fei <span style="color: black;">| 20 |</span> M <span style="color: black;">| 1000 |</span>
+------------+-----------+------+------+--------+
<span style="color: black;">1</span> row <span style="color: black;">in</span> set (<span style="color: black;">0</span>.<span style="color: black;">00</span> sec)
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">4、</span><span style="color: black;">查找</span>数据</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python<span style="color: black;">查找</span>Mysql<span style="color: black;">运用</span> fetchone() <span style="color: black;">办法</span>获取单条数据, <span style="color: black;">运用</span>fetchall() <span style="color: black;">办法</span>获取多条数据。</p>fetchone(): 该<span style="color: black;">办法</span>获取下一个<span style="color: black;">查找</span>结果集。结果集是一个对象fetchall(): 接收<span style="color: black;">所有</span>的返回结果行.rowcount: 这是一个只读属性,并返回执行 execute()<span style="color: black;">办法</span>后影响的行数。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">查找</span>数据代码如下:</strong></p><span style="color: black;">import</span>pymysql<span style="color: black;"># 打开数据库连接</span>
conn = pymysql.connect(
host=<span style="color: black;">"127.0.0.1"</span>,
user=<span style="color: black;">"root"</span>,
password=<span style="color: black;">"123456"</span>,
database=<span style="color: black;">"test_db"</span>,
charset=<span style="color: black;">"utf8"</span>)
<span style="color: black;"># 获取连接下的游标</span>
cursor_test = conn.cursor()
sql = <span style="color: black;">"""
select * from user1"""</span>
<span style="color: black;">try</span>:
<span style="color: black;"># 执行 sql 语句</span>
cursor_test.execute(sql)
<span style="color: black;"># <span style="color: black;">表示</span>出所有数据</span>data_result = cursor_test.fetchall()<span style="color: black;">for</span> row <span style="color: black;">in</span> data_result:
fname = row[<span style="color: black;">0</span>]
lname = row[<span style="color: black;">1</span>]
age = row[<span style="color: black;">2</span>]
sex = row[<span style="color: black;">3</span>]
income = row[<span style="color: black;">4</span>]
<span style="color: black;"># 打印结果</span>
print(<span style="color: black;">"fname=%s,lname=%s,age=%s,sex=%s,income=%s"</span> % \
(fname, lname, age, sex, income))
<span style="color: black;">except</span>:
print(<span style="color: black;">"Error: unable to fetch data"</span>)
<span style="color: black;"># 关闭数据库连接</span>
conn.close()
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">输出结果:</strong></p><span style="color: black;">fname</span>=Fei,lname=Fei,age=<span style="color: black;">20</span>,sex=M,income=<span style="color: black;">1000.0</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">5、</span>数据库表更新操作</h3><span style="color: black;"># 导入模块</span>
import pymysql
<span style="color: black;"># 打开数据库连接</span>
conn = pymysql.connect(
host=<span style="color: black;">"127.0.0.1"</span>,
user=<span style="color: black;">"root"</span>,
password=<span style="color: black;">"123456"</span>,
database=<span style="color: black;">"test_db"</span>,
charset=<span style="color: black;">"utf8"</span>)
<span style="color: black;"># print(conn)</span>
<span style="color: black;"># print(type(conn))</span>
<span style="color: black;"># 获取连接下的游标</span>
cursor_test = conn.cursor()
sql = <span style="color: black;">"UPDATE user1 SET AGE = AGE + 1 WHERE SEX = %c"</span> % (M)
<span style="color: black;">try:</span>
<span style="color: black;"># 执行SQL语句</span>
cursor_test.execute(sql)
<span style="color: black;"># 提交到数据库执行</span>
conn.commit()
<span style="color: black;">except:</span>
<span style="color: black;"># <span style="color: black;">出现</span>错误时回滚</span>
conn.rollback()
<span style="color: black;"># 关闭数据库连接</span>
conn.close()
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">6、</span>删除操作</h3><span style="color: black;"># 导入模块</span>
import pymysql
<span style="color: black;"># 打开数据库连接</span>
conn = pymysql.connect(
host=<span style="color: black;">"127.0.0.1"</span>,
user=<span style="color: black;">"root"</span>,
password=<span style="color: black;">"123456"</span>,
database=<span style="color: black;">"test_db"</span>,
charset=<span style="color: black;">"utf8"</span>)
<span style="color: black;"># print(conn)</span>
<span style="color: black;"># print(type(conn))</span>
<span style="color: black;"># 获取连接下的游标</span>
cursor_test = conn.cursor()
sql = <span style="color: black;">"DELETE * FROM user1"</span>
<span style="color: black;">try:</span>
<span style="color: black;"># 执行SQL语句</span>
cursor_test.execute(sql)
<span style="color: black;"># 提交到数据库执行</span>
conn.commit()
<span style="color: black;">except:</span>
<span style="color: black;"># <span style="color: black;">出现</span>错误时回滚</span>
conn.rollback()
<span style="color: black;"># 关闭数据库连接</span>
conn.close()
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">7、</span>错误处理</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DB API中定义了<span style="color: black;">有些</span>数据库操作的错误及<span style="color: black;">反常</span>,下表列出了这些错误和<span style="color: black;">反常</span>:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/e80a664c4c2f4f10a8c6a8bffe4ff218~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723899495&x-signature=0y1MxAfhvHcpq92I7avl7PFFpIc%3D" style="width: 50%; margin-bottom: 20px;"></div>
你的见解独到,让我受益匪浅,期待更多交流。 我深感你的理解与共鸣,愿对话长流。 在遇到你之前,我对人世间是否有真正的圣人是怀疑的。 外贸B2B平台有哪些? 网站建设seio论坛http://www.fok120.com/ 期待与你深入交流,共探知识的无穷魅力。 i免费外链发布平台 http://www.fok120.com/ 回顾历史,我们不难发现:无数先辈用鲜血和生命铺就了中华民族复兴的康庄大道。
页:
[1]