在Python3中,使用pymysql操作MySQL数据库数据。此外,还有Python官方提供的mysql-connector,以及第三方的MySQLdb(py2)和pymysql(py3)。
pymysql操作MySQL数据库的流程有几个流程要点:
1. 引入pymysql API模块
2. 获取与MySQL Server的连接地址
3. 获取游标(cursor)
4. 执行SQL或存储过程
5. 关闭游标
6. 关闭连接
一、安装pymysql模块
1pip install pymysql
二、游标以及连接池概念
游标:是处理数据的一种方法,为查看或处理结果集中数据,游标提供了在结果集中一次一行或多行前进或向后浏览数据的能力。可以把游标当作是一个指针,它可以指定结果中的任何位置,然后允许用户对指定位置的数据进行处理。通俗的说,就是操作数据和获取数据库结果都需要通过游标来操作。
1
2
3
4
5
6
7
8
9conn = mysql ip #获取MySQ Server地址,注意charset等参数说明 cursor = conn.cursor() #创建游标,游标是可以移动的,cursor.scroll() cursor.execute(sql) #执行SQL cursor.fetchone() #获取数据 cursor.fetchall() #获取数据 cursor.commit() #提交数据 cursor.rollback() #回滚数据 cursor.close() #关闭游标 conn.close() #关闭连接,相当于mysql>exit
连接池:应用端通过数据库连接池连接db,数据库连接池其实就是喝数据库建立了持续的TCP连接,从而达到数据库连接的资源复用。
三、pymysql连接MySQL数据库
1
2
3
4
5
6
7
8import pymysql mysqlconn = pymysql.connect(host='192.168.26.173', user='python', password='passwd', port=3306, database='test', charset='utf8') cursor = mysqlconn.cursor()
四、pymysql操作MySQL数据
4.1 创建一张测试表employee
1
2
3cursor.execute("drop table if exists employee") create_sql = "create table employee(first_name varchar(20) not null, last_name varchar(20), age int, sex char(1), income float )" cursor.execute(create_sql)
4.2 插入employee一条数据
1
2
3
4
5
6
7
8
9
10
11insert_sql = "insert into employee(first_name, last_name, age, sex, income) values('Mac', 'Moban', 20, 'M', 30000)" try: cursor.execute(insert_sql) mysqlconn.commit() print("数据库插入成功!") except: mysqlconn.rollback() print("数据库插入失败!")
4.3 获取(查询)数据
fetchone():获取一行数据,返回一个元组数据(即一维元组),如果没有数据,则返回None,即MySQL的null
fetchall():获取所有行的数据,返回一组元组数据(即二维元组),如果没有数据,则返回()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26select_sql = 'select * from employee where income>{}'.format(20000) try: ''' 以下测试fetchone和fetchall的区别是啥? 1. 区别一如下: fetchone: <class 'tuple'> ('Mac', 'Moban', 20, 'M', 30000.0) ERROR: unable to fetch data! fetchall: <class 'tuple'> (('Mac', 'Moban', 20, 'M', 30000.0),) ''' cursor.execute(select_sql) results = cursor.fetchone() print(results) print("fname = {},lname = {}, age = {}, sex = {}, income = {}".format(results[0], results[1], results[2], results[3], results[4])) except: print("ERROR: unable to fetch data!") ('Mac', 'Moban', 20, 'M', 30000.0) fname = Mac,lname = Moban, age = 20, sex = M, income = 30000.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30select_sql = 'select * from employee02 where income>{}'.format(20000) try: ''' 以下测试fetchone和fetchall的区别是啥?为啥以下的for循环对fetchone不管用了? 1. 区别一如下: fetchone: <class 'tuple'> ('Mac', 'Moban', 20, 'M', 30000.0) ERROR: unable to fetch data! fetchall: <class 'tuple'> (('Mac', 'Moban', 20, 'M', 30000.0),) ''' cursor.execute(select_sql) results = cursor.fetchall() print(results) for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] print("fname = {},lname = {}, age = {}, sex = {}, income = {}".format(fname, lname, age, sex, income)) except: print("ERROR: unable to fetch data!") (('Mac', 'Moban', 20, 'M', 30000.0),) fname = Mac,lname = Moban, age = 20, sex = M, income = 30000.0
4.4 增删改操作参考4.3的查询获取操作,加上一些cursor.commit()或cursor.rollback()操作
几点待深入理解:
1. pymysql连接池的使用
2. 一维和二维元组
3. 如何把pymysql增删改查操作封装一个类
4. pymysql防sql注入
5. pymysql默认select获取得到的数据是元组类型,如何转化为字典等类型
6. 获取多行以及执行多行操作 fetchmany() executemany()
7. 获取自增id
8. 游标移动 cursor.scroll()
【参考】
Python中操作mysql的pymysql模块详解 - 明天OoO你好 - 博客园
Python之pymysql的使用 - liubinsh - 博客园
最后
以上就是英勇铅笔最近收集整理的关于Python3-22/456-004 Python操作MySQL的全部内容,更多相关Python3-22/456-004内容请搜索靠谱客的其他文章。
发表评论 取消回复