python开发11之PyMySQL模块

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: python开发11之PyMySQL模块 1.PyMySQL安装  1.1通过pypi安装PyMySQL模块  • pypi即python package index  • 是python语言的软件仓库通过pip安装PyMySQL模块  • 官方站点为https://pypi.

python开发11之PyMySQL模块

1.PyMySQL安装 
1.1通过pypi安装PyMySQL模块 
• pypi即python package index 
• 是python语言的软件仓库通过pip安装PyMySQL模块 
• 官方站点为https://pypi.python.org

方法一:官网下载安装包,本地安装
[root@miss  ~]#yum install  -y  gcc     //安装依赖包
[root@miss  ~]#pip3  install  PyMySQL-0.9.0.tar.gz 

方法二:在线安装
[root@miss  ~]#pip3  install   pymysql 

方法三:使用国内镜像站点,为了实现安装加速,可以配置pip安装时采用国内镜像站点
[root@miss ~]#mkdir    ~/.pip/    
[root@miss ~]#vim  ~/.pip/pip.conf        
[global]    
index-url=http://pypi.douban.com/simple/    
[install]   
trusted-host=pypi.douban.com    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2.PyMySQL应用 
2.1 连接数据库 
• 创建连接是访问数据库的第一步 
conn=pymysql.connect(host=’127.0.0.1’,port=3306,user=’root’,passwd=’123456’, db=’db1’, charset=’utf8’) 
2.2 游标 
• 游标(cursor)就是游动的标识 
• 通俗的说,一条sql取出对应n条结果资源的接口/句柄,就 
是游标,沿着游标可以一次取出一行 
cursor = conn.cursor() 
2.3 插入数据 
• 对数据库表做修改操作,必须要commit 
sql1 = “insert into departments(dep_name) values(%s)” 
result = cur.execute(sql1, (‘development’,))

sql2 = “insert into departments(dep_name) values(%s)” 
data = [(‘hr’,), (‘op’,)] 
result = cur.executemany(sql2, data)

sql3 = “insert into departments(dep_name) values(%s)” 
data = [(‘行政’,), (‘财务’,), (‘运营’,)] 
result = cur.executemany(sql3, data)

conn.commit() 
2.4查询数据 
• 可以取出表中一条、多条或全部记录 
sql4 = “select * from departments” 
cur.execute(sql4) 
result = cur.fetchone() 
print(result)

result2 = cur.fetchmany(2) 
print(result2)

result3 = cur.fetchall() 
print(result3) 
2.5移动游标 
• 如果希望不是从头取数据,可以先移动游标 
cur.scroll(1, mode=”rala\ve”) 
cur.scroll(2, mode=”absolute”)

sql5 = “select * from departments” 
cur.execute(sql5) 
cur.scroll(3, mode=’absolute’) 
result4 = cur.fetchmany(2) 
print(result4) 
2.6 修改数据 
• 通过update修改某一字段的值 
sql6 = “update departments set dep_name=%s where dep_name=%s” 
result = cur.execute(sql6, (‘opera\ons’, ‘op’)) 
print(result) 
conn.commit() 
2.7 删除记录 
• 通过delete删除记录 
sql7 = “delete from departments where dep_id=%s” 
result = cur.execute(sql7, (6,)) 
print(result) 
conn.commit() 
3.案例 
发工资的数据库 
姓名、性别、出生年月、部门、联系方式、员工编号、发工资日期、基本工资、奖金、总工资 
第一范式(1NF):所有的域都应该是原子性的,即数据库表的每一列都是不可分割的原子数据项 
联系方式应该拆分为:住址、电话号码、email等 
第二范式(2NF):在1NF的基础上,非码属性必须完全依赖于候选码 
把字段放到不同的表里: 
员工表:员工编号、姓名、性别、出生年月、部门ID、电话号码、email 
部门表:部门ID、部门编号 
工资表:autoid、员工编号、发工资日期、基本工资、奖金、总工资 
第三范式(3NF):在2NF基础上,任何非主属性不依赖于其它非主属性(在2NF基础上消除传递依赖) 
因为总工资是用基本工资和奖金算出来的,所以它不要出现在数据库表中

[root@miss  ~]#mysql -uroot -p123456
MariaDB [(none)]> CREATE DATABASE db1 DEFAULT CHARSET='utf8';
MariaDB [(none)]> use db1;
MariaDB [db1]> CREATE TABLE departments
(dep_id INT, dep_name VARCHAR(20), PRIMARY KEY(dep_id));
MariaDB [db1]> CREATE TABLE employees
(emp_id INT, emp_name VARCHAR(20) NOT NULL, gender VARCHAR(6), email VARCHAR(50), dep_id INT, PRIMARY KEY(emp_id), FOREIGN KEY(dep_id) REFERENCES departments(dep_id));
MariaDB [db1]>  CREATE TABLE salary
(auto_id INT AUTO_INCREMENT, date DATE, emp_id INT, basic INT, awards INT, PRIMARY KEY(auto_id), FOREIGN KEY(emp_id) REFERENCES employees(emp_id));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
[root@miss  ~]#cat mysql_data.py
import pymysql

conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='123456',db='db1',charset='utf8')            # 创建到数据库的连接

cursor = conn.cursor()  # 创建游标,相当于打开文件返回文件对象
insert_dep1 = 'INSERT INTO departments VALUES(%s, %s)'
# cursor.execute(insert_dep1, ('1', '人事部'))
insert_deps = [(2, '运维部'), (3, '开发部'), (4, '测试部')]
cursor.executemany(insert_dep1, insert_deps)
conn.commit()  # 增删改都需要commit
cursor.close()
conn.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
[root@miss  ~]#cat mysql_modify.py
import pymysql

conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='123456',db='db1',charset='utf8')            # 创建到数据库的连接

cursor = conn.cursor()  # 创建游标,相当于打开文件返回文件对象
insert_dep1 = 'UPDATE departments SET dep_name=%s WHERE dep_name=%s'
cursor.execute(insert_dep1, ('人力资源部', '人事部'))
delete1 = 'DELETE FROM departments WHERE dep_name=%s'
cursor.execute(delete1, ('测试部',))
conn.commit()  # 
cursor.close()
conn.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
[root@miss  ~]#cat mysql_query1.py
import pymysql

conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='123456',db='db1',charset='utf8')            # 创建到数据库的连接
cursor = conn.cursor()

query1 = 'SELECT * FROM departments'
cursor.execute(query1)
r1 = cursor.fetchone()
print(r1)
print('#' * 20)
r2 = cursor.fetchmany(2)
print(r2)
print('#' * 20)
r3 = cursor.fetchall()
print(r3)

cursor.close()
conn.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
[root@miss  ~]#cat mysql_query2.py
import pymysql

conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='123456',db='db1',charset='utf8')            # 创建到数据库的连接
cursor = conn.cursor()
query1 = 'SELECT * FROM departments'
cursor.execute(query1)
# cursor.scroll(2, mode='absolute')
# r1 = cursor.fetchall()
# print(r1)
cursor.scroll(1, mode='absolute')  # 以开头为起始点移动游标
cursor.fetchone()  # 取出一行
cursor.scroll(1, mode='relative')  # 以当前位置为参考点移动游标
r2 = cursor.fetchall()  # 取出后续所有内容
print(r2)

cursor.close()
conn.close(
原文地址https://blog.csdn.net/Echo_Blingbling/article/details/82118905
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1天前
|
开发者 Python
Python的os模块详解
Python的os模块详解
11 0
|
4天前
|
数据挖掘 API 数据安全/隐私保护
python请求模块requests如何添加代理ip
python请求模块requests如何添加代理ip
|
6天前
|
前端开发 Java Go
开发语言详解(python、java、Go(Golong)。。。。)
开发语言详解(python、java、Go(Golong)。。。。)
|
6天前
|
测试技术 Python
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
|
6天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
39 1
|
8天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
49 0
|
9天前
|
Python
python学习14-模块与包
python学习14-模块与包
|
9天前
|
前端开发 数据挖掘 API
使用Python中的Flask框架进行Web应用开发
【4月更文挑战第15天】在Python的Web开发领域,Flask是一个备受欢迎的轻量级Web框架。它简洁、灵活且易于扩展,使得开发者能够快速地构建出高质量的Web应用。本文将深入探讨Flask框架的核心特性、使用方法以及在实际开发中的应用。
|
数据库 Python
基于python的pymysql模块实现向数据库中插入一条数据
基于python的pymysql模块实现向数据库中插入一条数据 基于业务需求需要向某表经常插入数据,故编辑了该脚本方便在堡垒机中直接向服务器插入数据,这里我们以一行为例。 程序介绍:  程序分为两个函数分别为插入函数和查询函数顾名思义就是实现数据插入和查询 效果:带参数执行该程序 程序会判断该表.
2141 0
|
13天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。

热门文章

最新文章