Python使用pymysql模块操作mysql增删改查实例分析
作者:dawn-liu 时间:2024-01-24 03:56:48
本文实例讲述了Python使用pymysql模块操作mysql增删改查。分享给大家供大家参考,具体如下:
# -*- coding:utf-8 -*-
import pymysql
user = input('请输入用户名:')
pwd = input('请输入密码:')
# 1.连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123', db='t1', charset='utf8')
print(conn)
# 2.创建游标
cursor = conn.cursor()
#注意%s需要加引号
sql = "select * from t1.userinfo where username='%s' and pwd='%s'" %(user, pwd)
print(sql)
# 3.执行sql语句
cursor.execute(sql)
result=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目
print(result)
# 关闭连接,游标和连接都要关闭
cursor.close()
conn.close()
if result:
print('登陆成功')
else:
print('登录失败')
下面是执行过程
请输入用户名:lisi
请输入密码:123
<pymysql.connections.Connection object at 0x000001BEFABFE240>
select * from t1.userinfo where username='lisi' and pwd='123'登陆成功
二、execute()之sql注入
方式
#1、sql注入之:用户存在,绕过密码
lisi' -- 任意字符
#2、sql注入之:用户不存在,绕过用户与密码
xxx' or 1=1 -- 任意字符
请输入用户名:sdj;fja;' or 1=1 -- ;j;j;jj;jjkdsjfjsd
请输入密码:123
<pymysql.connections.Connection object at 0x000001EF2BE3E240>
select * from t1.userinfo where username='sdj;fja;' or 1=1 -- ;j;j;jj;jjkdsjfjsd' and pwd='123'登陆成功
解决:
1采用列表的方式
# 原来是我们对sql进行字符串拼接
# sql="select * from userinfo where name='%s' and password='%s'" %(username,pwd)
# print(sql)
# result=cursor.execute(sql)
#改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了)
sql="select * from userinfo where name=%s and pwd=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上
result=cursor.execute(sql,[user,pwd]) #pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。
2采用字典的方法
# -*- coding:utf-8 -*-
import pymysql
user = input('请输入用户名').strip()
pwd = input('请输入密码').strip()
# 连接服务端
conn = pymysql.connect(
host='127.0.0.1',
user='root',
password="123",
database='t1',
port=3306,
charset='utf8'
)
# -- ddadad
# 创建游标对象
cur = conn.cursor()
sql = "select * from userinfo where username = %(name)s and pwd = %(password)s"
print(sql)
# resultNum = cur.execute(sql,[user,pwd])
resultNum = cur.execute(sql,{"name":user,"password":pwd})
print(resultNum)
cur.close()
conn.close()
if resultNum:
print('登陆成功')
else:
print('登陆失败')
三、增、删、改:conn.commit()
commit()方法:在数据库里增、删、改的时候,必须要进行提交,否则插入的数据不生效。
基本框架
import pymysql
username = input('请输入用户名:')
pwd = input('请输入密码:')
# 1.连接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='123', db='db8', charset='utf8')
# 2.创建游标
cursor = conn.cursor()
--------增删改操作----------------
#一定记得commit
conn.commit()
# 4.关闭游标
cursor.close()
# 5.关闭连接
conn.close()
增--》一组数据
sql='insert into userinfo(username,pwd) values(%s,%s)'
effect_row=cursor.execute(sql,(username,pwd)) # effect_row=1
增---》多组数据
sql='insert into userinfo(username,pwd) values(%s,%s)'
effect_row=cursor.executemany(sql,[('赵八','112'),('刘九','114'),('封十','911')]) #effect_row=3
删
sql='delete from userinfo where pwd like "%2"'
effect_row=cursor.execute(sql)
改
sql='update userinfo set username = %s where pwd="114"'
effect_row=cursor.execute(sql,'niu') #effect_row=2
**上面的变量 effect_row=cursor.execute(...) ,返回的是成功改变的条目数字
四、查:fetchone、fetchmany、fetchall
表的内容
mysql> select * from userinfo;
+----+----------+-----+
| id | username | pwd |
+----+----------+-----+
| 1 | mjj | 123 |
| 3 | 张三 | 110 |
| 4 | 李四 | 119 |
+----+----------+-----+
3 rows in set (0.00 sec)
固定格式
import pymysql
# 1.连接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8')
# 2.创建游标
cursor = conn.cursor()
sql = 'select * from userinfo'
cursor.execute(sql)
------------执行的查询--------------
# 4.关闭游标
cursor.close()
# 5.关闭连接
conn.close()
fetchone查看一条符合条件的数据,可以连续使用,查询的是上一个fetchone的后面一条
# 查询第一行的数据
row = cursor.fetchone()
print(row) # (1, 'mjj', '123')
# 查询第二行数据
row = cursor.fetchone()
print(row) # (3, '张三', '110')
fetchall():查询所有符合条件的数据
# 获取所有的数据
rows = cursor.fetchall()
print(rows)
#运行结果
((1, 'mjj', '123'), (3, '张三', '110'), (4, '李四', '119')) 取到的返回值是元组
fetchmany:获取指定的条数数据
row=cursor.fetchmany(3)
print(row)
cursor.scroll(num,mode='relative|absolute') 当mode=absolute时,num不能小于0
cursor.scroll(1,mode='relative') # 相对当前位置移动
cursor.scroll(2,mode='absolute') # 相对绝对位置移动
# 查询第一行的数据
row = cursor.fetchone()
print(row) # (1, 'mjj', '123')
# 查询第二行数据
row = cursor.fetchone() # (3, '张三', '110')
print(row)
cursor.scroll(-1,mode='relative') #设置之后,光标相对于当前位置往前移动了一行,所以打印的结果为第二行的数据
row = cursor.fetchone()
print(row)
cursor.scroll(0,mode='absolute') #设置之后,光标相对于首行没有任何变化,所以打印的结果为第一行数据
row = cursor.fetchone()
print(row)
希望本文所述对大家Python程序设计有所帮助。
来源:https://www.cnblogs.com/mmyy-blog/p/9629224.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP入门教程之自定义函数用法详解(创建,调用,变量,参数,返回值等)
Tornado Application的实现
如何用Matlab和Python读取Netcdf文件
![](https://img.aspxhome.com/file/2023/8/90448_0s.png)
BatchNorm2d原理、作用及pytorch中BatchNorm2d函数的参数使用
![](https://img.aspxhome.com/file/2023/1/118661_0s.jpg)
PHP三层结构(上) 简单三层结构
![](https://img.aspxhome.com/file/2023/6/553461_0s.png)
Python中的super()方法使用简介
goland 实现自动格式化代码
![](https://img.aspxhome.com/file/2023/3/110523_0s.jpg)
详解从vue的组件传值着手观察者模式
![](https://img.aspxhome.com/file/2023/3/126413_0s.jpg)
纯CSS在Firefox模拟text-overflow: ellipsis效果
![](https://img.aspxhome.com/file/UploadPic/20093/17/firefox-css-text-overflow-21s.gif)
Python实战项目刮刮乐的实现详解流程
![](https://img.aspxhome.com/file/2023/4/103204_0s.png)
Autopep8的使用(python自动编排工具)
![](https://img.aspxhome.com/file/2023/7/96967_0s.png)
利用Psyco提升Python运行速度
Python+Turtle动态绘制一棵树实例分享
![](https://img.aspxhome.com/file/2023/6/76866_0s.gif)
javascript获取select值的方法完整实例
![](https://img.aspxhome.com/file/2023/0/136090_0s.gif)
详解Python中字典的增删改查
![](https://img.aspxhome.com/file/2023/5/131025_0s.jpg)
用户凭什么跟你注册?
MySQL中Multiple primary key defined报错的解决办法
![](https://img.aspxhome.com/file/2023/4/76454_0s.png)
使用 FFmpeg 命令拼接mp3音频文件异常问题及解决方法
![](https://img.aspxhome.com/file/2023/5/127235_0s.jpg)
scrapy处理python爬虫调度详解
jQuery自适应轮播图插件Swiper用法示例
![](https://img.aspxhome.com/file/2023/6/135856_0s.png)