python开发11之PyMySQL模块
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
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Kotlin 全栈开发之 前端 JavaScript: Getting Started with Kotlin and JavaScrip...
Kotlin 全栈开发之 前端 JavaScript: Getting Started with Kotlin and JavaScript with Gradle 完整工程源代码: https://github.com/Jason-Chen-2017/kotlin-js-gradle-demo image.png HelloWorld.kt package demo import kotlin.browser.document import kotlin.js.Date fun say() { println("Hello,World") } fun getDate(n: Int): Date { var d = Date() var ntime = d.getTime() + n * 24 * 3600 * 1000 // ms return Date(ntime) } fun renderTable() { val e = document.getElementById("title") e?.innerHTML = """ <h1>Kotlin VS. JavaSc...
- 下一篇
Windows下如何查看某个端口被谁占用
Windows下如何查看某个端口被谁占用 一、查看那个端口被调用 我告诉大家一个方法,^_^。 1、 开始—->运行—->cmd,或者是window+R组合键,调出命令窗口; 2、输入命令:netstat -ano,列出所有端口的情况。在列表中我们观察被占用的端口,比如是1224,首先找到它; 3、查看被占用端口对应的PID,输入命令:netstat -aon|findstr “8081”,回车,记下最后一位数字,即PID,这里是9088; 4、 继续输入tasklist|findstr “9088”,回车,查看是哪个进程或者程序占用了8081端口,结果是:node.exe 或者是我们打开任务管理器,切换到进程选项卡,在PID一列查看9088对应的进程是谁,如果看不到PID这一列,如下图: 之后我们就可以结束掉这个进程了~~ 也可以用命令行杀死进程: taskkill /pid 1244 -f 在命令行窗口直接写tasklist可以查看进程 二、win7命令行强制杀死进程 原理 1、查看当前正在运行的进程tasklist 如下图所示 2、强制杀死映像名称为imag...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8编译安装MySQL8.0.19
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Red5直播服务器,属于Java语言的直播服务器
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS6,CentOS7官方镜像安装Oracle11G
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装