pymysql模块
1.使用步骤
-
导入pymysql模块
import pymysql
-
连接数据库
conn = pymysql.connect( host = '127.0.0.1', port = 3306, user = 'root' password = 'mysql123', database = 'exercise', charset = 'utf-8' )
-
获取光标对象
-
获取元组类型的光标(默认)
-
获取字典类型的光标
# 光标为元组的形式 cursor = conn.cursor() # 把光标改变为字典的形式 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
-
-
执行SQL语句
sql = 'sql语句' # 执行语句 cursor.execute(sql,[sql语句的拼接参数])
- 注意:sql语句自主随意拼接会出现SQL注入的问题,导致了信息不安全的情况,所以为了避免此问题,必须对输入内容做检测,在pymysql内封装了此功能,所有的拼接工作都在执行SQL语句阶段以列表的形式传入,由此避免了SQL的注入。
-
关闭
- 关闭光标
cursor.close()
- 关闭连接
conn.close()
- 关闭光标
2.数据库的增删改查
-
增
-
基本操作
sql = 'insert into userinfo(username,password) values (%s,%s)' cursor.execute(sql,['kk','kkk']) # 执行SQL语句,并传入参数 conn.commit() #上传更改的数据
-
插入数据失败回滚
- 出现情况当插入数据不成功时,报错时,利用
conn.rollback()
执行回滚操作。
- 出现情况当插入数据不成功时,报错时,利用
-
获取插入数据的ID值
-
在关联表查询时有可能会使用到,可以获得插入数据的ID值
-
实例
sql = 'insert into userinfo(username,password) values (%s,%s)' cursor.execute(sql,['abc','abc']) # 执行SQL语句 conn.commit() # 上传更改的数据 last_id = cursor.lastrowid # 输入插入的ID值 print(last_id) #输出:6 ``` # 数据库数据,插入的abc对应主键id为6 +----+----------+----------+ | id | username | password | +----+----------+----------+ | 1 | abner | 123 | | 2 | marry | 456 | | 3 | sunny | 789 | | 4 | dam | 000 | | 5 | kk | kkk | | 6 | abc | abc | +----+----------+----------+ ```
-
-
批量插入数据
-
实例:
sql = 'insert into userinfo(username,password) values (%s,%s)' data = [('rain','123'),('cc','aaa'),('vivi','vvv')] # 要插入的多条数据 cursor.executemany(sql,data) # 批量执行SQL语句 conn.commit() #上传更改的数据 # 数据库插入结果: +----+----------+----------+ | id | username | password | +----+----------+----------+ | 1 | abner | 123 | | 2 | marry | 456 | | 3 | sunny | 789 | | 4 | dam | 000 | | 5 | kk | kkk | | 6 | abc | abc | | 7 | rain | 123 | | 8 | cc | aaa | | 9 | vivi | vvv | +----+----------+----------+
-
注意:执行一条语句使用execute(),批量执行使用executemany(sql,data),数据必须以列表套元组的形式传入
-
-
-
删
-
pymysql模块中的删和数据库内的删除一样,唯一的区别在于必须上传数据的更新。
# 删除username为'kk'的行 sql = "delete from userinfo where username='kk'" cursor.execute(sql) conn.commit() #上传更改的数据 # 数据库处理的结果: +----+----------+----------+ | id | username | password | +----+----------+----------+ | 1 | abner | 123 | | 2 | marry | 456 | | 3 | sunny | 789 | | 4 | dam | 000 | | 6 | abc | abc | | 7 | rain | 123 | | 8 | cc | aaa | | 9 | vivi | vvv | +----+----------+----------+
-
-
改
-
和MySQL环境里类似,同理在pymysql中需要更新上传数据。
sql = 'update userinfo set password=%s where username=%s;' cursor.execute(sql,['111','abner']) conn.commit() #上传更改的数据 # 数据库的结果: +----+----------+----------+ | id | username | password | +----+----------+----------+ | 1 | abner | 111 | | 2 | marry | 456 | | 3 | sunny | 789 | | 4 | dam | 000 | | 6 | abc | abc | | 7 | rain | 123 | | 8 | cc | aaa | | 9 | vivi | vvv | +----+----------+----------+
-
-
查
-
查询单条数据
sql = 'select * from userinfo;' cursor.execute(sql) conn.commit() #上传更改的数据 ret = cursor.fetchone() # 查询一条数据 print(ret) # 输出:{'id': 1, 'username': 'abner', 'password': '111'}
- 查询一条数据使用
cursor.fetchone()
- 查询一条数据使用
-
查询多条数据
- 查询全部数据
sql = 'select * from userinfo;' cursor.execute(sql) conn.commit() #上传更改的数据 ret = cursor.fetchall() # 查询全部数据 print(ret) # 输出:[{'id': 1, 'username': 'abner', 'password': '111'}, {'id': 2, 'username': 'marry', 'password': '456'}, {'id': 3, 'username': 'sunny', 'password': '789'}, {'id': 4, 'username': 'dam', 'password': '000'}, {'id': 6, 'username': 'abc', 'password': 'abc'}, {'id': 7, 'username': 'rain', 'password': '123'}, {'id': 8, 'username': 'cc', 'password': 'aaa'}, {'id': 9, 'username': 'vivi', 'password': 'vvv'}]
- 查询指定条数数据
sql = 'select * from userinfo;' cursor.execute(sql) conn.commit() #上传更改的数据 ret = cursor.fetchmany(3) # 查询三条数据 print(ret) # 输出:[{'id': 1, 'username': 'abner', 'password': '111'}, {'id': 2, 'username': 'marry', 'password': '456'}, {'id': 3, 'username': 'sunny', 'password': '789'}]
-
高阶用法
-
光标的绝对移动
sql = 'select * from userinfo;' cursor.execute(sql) conn.commit() #上传更改的数据 ret = cursor.fetchmany(2) # 查询两条数据 print(ret) ret = cursor.fetchmany(2) # 查询两条数据 print(ret) ret = cursor.fetchmany(2) # 查询两条数据 print(ret) ret = cursor.fetchmany(2) # 查询两条数据 print(ret) cursor.scroll(0,mode='absolute') # 将光标移动到开头 ret = cursor.fetchone() # 查询一条数据 print(ret) # 输出: [{'id': 1, 'username': 'abner', 'password': '111'}, {'id': 2, 'username': 'marry', 'password': '456'}] [{'id': 3, 'username': 'sunny', 'password': '789'}, {'id': 4, 'username': 'dam', 'password': '000'}] [{'id': 6, 'username': 'abc', 'password': 'abc'}, {'id': 7, 'username': 'rain', 'password': '123'}] [{'id': 8, 'username': 'cc', 'password': 'aaa'}, {'id': 9, 'username': 'vivi', 'password': 'vvv'}] {'id': 1, 'username': 'abner', 'password': '111'}
-
光标的相对移动
sql = 'select * from userinfo;' cursor.execute(sql) conn.commit() #上传更改的数据 ret = cursor.fetchmany(2) # 查询两条数据 print(ret) ret = cursor.fetchmany(2) # 查询两条数据 print(ret) ret = cursor.fetchmany(2) # 查询两条数据 print(ret) ret = cursor.fetchmany(2) # 查询两条数据 print(ret) cursor.scroll(-1,mode='relative') # 将光标移动到现在位置的上一个位置 ret = cursor.fetchone() # 查询一条数据 print(ret) # 输出: [{'id': 1, 'username': 'abner', 'password': '111'}, {'id': 2, 'username': 'marry', 'password': '456'}] [{'id': 3, 'username': 'sunny', 'password': '789'}, {'id': 4, 'username': 'dam', 'password': '000'}] [{'id': 6, 'username': 'abc', 'password': 'abc'}, {'id': 7, 'username': 'rain', 'password': '123'}] [{'id': 8, 'username': 'cc', 'password': 'aaa'}, {'id': 9, 'username': 'vivi', 'password': 'vvv'}] {'id': 9, 'username': 'vivi', 'password': 'vvv'}
-
-
SQL注入问题
1.什么是SQL注入?
- 用户输入的内容有恶意的SQL语句,后端拿到用户输入的内容不做检查直接做字符串拼接,得到一个和预测不一致的结果。
2.如何解决注入问题?
- 对用户的输入内容做检查
- 在pymysql中提供了定义好的占位符,可以让模块帮助拼接语句,但是在pymysql内只有一种占位符
%s
,在execute执行时,第二个参数以列表的形式传参拼接SQL语句。
#执行SQL语句
sql = "select * from userinfo where username=%s and password=%s"
#使用光标对象执行SQL语句
ret = cursor.execute(sql,[username,password])