• MySQL参数化有效防止SQL注入


    • sql语句的参数化,可以有效防止sql注入
    • 注意:此处不同于python的字符串格式化,全部使用%s占位
      from pymysql import *
      
      def main():
      
          find_name = input("请输入物品名称:")
      
          # 创建Connection连接
          conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
          # 获得Cursor对象
          cs1 = conn.cursor()
      
      
          # # 非安全的方式
          # # 输入 " or 1=1 or "   (双引号也要输入)
          # sql = 'select * from goods where name="%s"' % find_name
          # print("""sql===>%s<====""" % sql)
          # # 执行select语句,并返回受影响的行数:查询所有数据
          # count = cs1.execute(sql)
      
          # 安全的方式
          # 构造参数列表
          params = [find_name]
          # 执行select语句,并返回受影响的行数:查询所有数据
          count = cs1.execute('select * from goods where name=%s', params)
          # 注意:
          # 如果要是有多个参数,需要进行参数化
          # 那么params = [数值1, 数值2....],此时sql语句中有多个%s即可 
      
          # 打印受影响的行数
          print(count)
          # 获取查询的结果
          # result = cs1.fetchone()
          result = cs1.fetchall()
          # 打印查询的结果
          print(result)
          # 关闭Cursor对象
          cs1.close()
          # 关闭Connection对象
          conn.close()
      
      if __name__ == '__main__':
          main()
  • 相关阅读:
    Laravel学习之旅(一)
    telnet模拟邮件发送
    学习CodeIgniter框架之旅(二)继承自定义类
    学习CodeIgniter框架之旅(一)自定义模板目录
    MySQL主从复制实现
    coreseek增量索引
    锁(MySQL篇)—之MyISAM表锁

    php文件锁
    进程与线程
  • 原文地址:https://www.cnblogs.com/john-xiong/p/11818639.html
Copyright © 2020-2023  润新知