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()

执行结果:

最新文章

  1. [LeetCode] Boom Enemy 炸弹人
  2. ipsec IP安全策略操作 win7
  3. PE注入
  4. POJ3020 匹配
  5. GridView的 OnRowDataBound 事件用法
  6. iOS开发--CoreGraphics简单绘图
  7. debian 学习记录-1 -安装
  8. Input File 表单上传按钮美化
  9. HDU2063(二分图最大匹配)
  10. BC#65T4 ZYB&#39;s Tree
  11. 安装haproxy和haproxy命令
  12. 使用PSR-4配合composer autoload 自动加载文件夹
  13. day14 十四、三元运算符,推导式,匿名内置函数
  14. video 铺满父元素(object-fit: fill;)
  15. WebAPI跨域处理
  16. [基础架构]PeopleSoft Tuxedo 重要文件说明
  17. 动态SQL是什么??什么是静态SQL,动态SQL的动态体现在哪里???
  18. 用 eric6 与 PyQt5 实现python的极速GUI编程(系列03)---- Drawing(绘图)(3)-- 画线
  19. 详解MathType中如何批量修改公式字体和大小
  20. Deep Learning(3)算法简介

热门文章

  1. 如何使用table布局静态网页
  2. Apifox(2)快速上手apifox
  3. python爬虫期末复习
  4. TCP链接请求的10种状态
  5. Tableau使用折线图和饼图的组合
  6. MH/T4029.3 IFPL报文解析
  7. CF979A Pizza, Pizza, Pizza!!! 题解
  8. Java 将PDF转为线性PDF
  9. 使用.NET 6开发TodoList应用文章索引
  10. .NET 多条件动态参数查询方法 - SqlSugar ORM