pymysql防止SQL注入的方法
2024-10-21 07:56:02
import pymysql class Db(object): def __init__(self):
self.conn = pymysql.connect(host="192.168.0.58", user="root", password="123456", port=3306)
self.cursor = self.conn.cursor() def __del__(self):
self.cursor.close()
self.conn.close() def execute_sql(self):
name = input("请输入被查询人的姓名:")
sql = F"select * from test1.user_info where name='{name}'"
print(f"----->{sql}<------")
self.cursor.execute(sql) # 自己拼装的SQL,在输入时使用特殊的传入值(比如:' or 1=1 or '1),能获取到数据表中的所有信息,也就是存在SQL注入的风险。
print(self.cursor.fetchall()) def secure_execute_sql(self):
name = input("(安全)请输入被查询人的姓名:")
sql = "select * from test1.user_info where name=%s"
# print(f"----->{sql}<------")
params = [name] # 将参数放入列表或元组中
self.cursor.execute(sql, params) # 使用execute方法拼装SQL语句,防SQL注入
print(self.cursor.fetchall()) def main():
db = Db()
while True:
db.execute_sql()
db.secure_execute_sql() if __name__ == "__main__":
main()
执行结果:
最新文章
- [LeetCode] Boom Enemy 炸弹人
- ipsec IP安全策略操作 win7
- PE注入
- POJ3020 匹配
- GridView的 OnRowDataBound 事件用法
- iOS开发--CoreGraphics简单绘图
- debian 学习记录-1 -安装
- Input File 表单上传按钮美化
- HDU2063(二分图最大匹配)
- BC#65T4 ZYB&#39;s Tree
- 安装haproxy和haproxy命令
- 使用PSR-4配合composer autoload 自动加载文件夹
- day14 十四、三元运算符,推导式,匿名内置函数
- video 铺满父元素(object-fit: fill;)
- WebAPI跨域处理
- [基础架构]PeopleSoft Tuxedo 重要文件说明
- 动态SQL是什么??什么是静态SQL,动态SQL的动态体现在哪里???
- 用 eric6 与 PyQt5 实现python的极速GUI编程(系列03)---- Drawing(绘图)(3)-- 画线
- 详解MathType中如何批量修改公式字体和大小
- Deep Learning(3)算法简介