您现在的位置是:首页 > 文章详情

python开发11之PyMySQL模块

日期:2018-08-29点击:538

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
原文链接:https://yq.aliyun.com/articles/630548
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章