zoukankan      html  css  js  c++  java
  • pymysql模块及sql注入

    pymysql模块

    sql注入

    pymysql模块应用

    1、连接数据库读取数据操作

    import pymysql
    conn = pymysql.connect(
      host='127.0.0.1',
      port=3306,
      user='root',
      password='123',
      database='day01',
      charset='utf8'
    )  # 连接数据库
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 产生一个`游标对象(用来帮助执行命令的)
    print(res)  # execute 返回的是当前sql语句所影响的行数 该返回值一般不用
    # 获取命令执行的查询结果
    # print(cursor.fetchone())  # 只拿一条
    # print(cursor.fetchall())  # 拿所有  # 读取文件类似光标移动
    # cursor.scroll(1,'relative')  # 相对于光标所在位置继续往后移动 1 位
    # cursor.scroll(1,'absolute')  # 相对于数据起始位置 往后移动 1 位
    # print(cursor.fetchmany(2))  # 指定拿几条
    
    

    2、增、删、改、查操作

    
    import pymysql
    conn = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        passwd='123',
        db='day04',
        charset='utf8'
    )
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    # 增
    # sql = 'insert into user(name,password) values(%s,%s)'
    # rows = cursor.execute(sql,('wudi',123))
    # print(rows)
    # 增多个
    rows = cursor.executemany(sql,[('xxx',123),('ooo',123),('yyy')])
    # 改
    sql = "update user set name='liuNB' where id =1"
    rows = cursor.execute(sql)
    print(rows)
    conn.commit()  # 确认
    # 删
    sql = 'delete from user where id = 1'
    rows = cursor.execute(sql)
    print(rows)
    # 查
    sql = 'select * from user'
    cursor.execute(sql)
    print(cursor.fetchall())
    

    增删改查中 不涉及数据的修改
    删、改、增 不能直接执行 需要二次确认 conn.commit() 二次确认
    可以在conn 中修改 autocommit = True

    sql注入
    
    # 结合数据库完成一个用户登录功能
    import pymysql
    conn = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='123',
        database='day04',
        charset='utf8'   # 编码不加 -
    )  # 连接数据库
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    username = input('>>>>:')
    password = input('>>>>:')
    # sql = "select * from user where name ='%s' and password= '%s' "%(username,password)
    sql = "select * from user where name =%s and password= %s"
    # 先用 % 占位后将需要拼接是数据直接交给 execute方法即可
    print(sql)
    res = cursor.execute(sql,(username,password)) # 自动识别%s 并且特殊符号不会有作用
    if res:
        print('登录成功')
        print(cursor.fetchall())
    else:
        print('用户名密码错误')
    

    select * from user where name ='liu' -- fasgasgsg' and password= ''

    select * from user where name ='xxx' or 1=1 -- dafafas' and password= ''

    特殊符号 -- 再mysql语句中是注释

    敏感数据不要自己做拼接

  • 相关阅读:
    java 菜单
    QT 让信号自由飞翔(骚操作)
    QT editLine 无法输入的问题
    易经初学体会
    Cgroup
    springboot pom 引用集合
    使用ab测试工具 进行并发测试
    intellij 设置-试验过的
    【iis错误码】IIS 服务 这些年遇到的错误码
    101个创业失败案例背后的20大原因
  • 原文地址:https://www.cnblogs.com/liuyang521/p/14443693.html
Copyright © 2011-2022 走看看