zoukankan      html  css  js  c++  java
  • SQL语句,pymysql模块,sql注入问题

    一、完整版SQL语句的查询

    select 
    	distinct post,avg(salary) 
    from 
    	table 
    where 
    	id > 1 
    group by
    	post`
    having 
    	avg(salary)>100
    order by 
    	avg(salary)
    limit 5,5
    

    ​ group by:分组之后,分组依据是最小可识别单位,不能再直接获取到其他字段信息,如果想要获取其他字段信息,只能用额外的方法间接获取,上述情况需要你设置严格模式,如果整个SQL语句没有group by默认整体就是一组。

    二、聚合函数

    • max
    • min
    • avg
    • sum
    • count

    ps:聚合函数只能在分组之后使用

    三、distinct和limit

    ​ distinct(去重)去重一定要满足数据是一模一样的情况下,才能达到去重的效果,如果你查询出来的数据中包含主键字段,那么不可能去重成功。

    ​ limit 5;只写一个参数,从第一条开始展示5条

    ​ limit 5,5;从第五条开始,不包含第五条,展示五条

    ps:MySQL对大小写不敏感

    四、模糊匹配和正则表达式匹配

    1、模糊匹配

    • %:多个任意字符

    • _:单个任意字符

    2、正则表达式

    where name regexp "^j.*(n|y)$"

    表示以j开头,n或y结尾,中间是任意字符

    .表示匹配除换行符之外的任意字符

    *表示o到无穷

    +表示1到无穷

    ?表示0或1

    ^表示以什么开头

    $表示以什么结尾

    回顾re模块

    findall:分组优先,会将括号内正则匹配到的优先返回

    match:从头开始匹配,匹配到一个就返回

    search:整体匹配,匹配到一个就返回

    res=match('^j.*(n|y)$','jason')

    print(res.group())

    五、concat和concat_ws

    concat和concat_ws在分组之前用

    • concat(name,':',age,':',salary) 用于拼接字符串

    • concat_ws(':',name,age,salary) 与上面的效果一致

    与字符串的join方法类似,但有区别:join方法只能用在字符串之间。

    ':'.join(['1',2,'3']) 由于2是数字,会报错

    六、exists

    ​ exists关键字表示存在,在使用exists关键字使,内层查询语句不返回查询的记录,而是返回一个真假值,True或False。

    ​ 当返回True时,外层查询语句将进行查询

    ​ 当返回False时,外层查询语句不进行查询

    select * from emp where exists (select id from dep where id>203);
    

    七、pymysql模块

    import pymysql
    
    conn = pymysql.connect(
    	host='127.0.0.1',
        port=3306,
        user='root',
        passwoord='123',
        database='day38',
        charset='utf8'  # 不能加—
    )
    cursor=conn.cursor(pymysql.cursors.DictCursor)  # 产生一个游标对象,以字典的形式返回查询结果
    sql = 'select * from teacher'
    res = cursor.execute(sql)  # 执行传入的sql语句
    print(res)  # res是执行语句返回的数据条数
    print(cursor.fetchone())  # 只获取一条数据
    print(cursor.fetchone())  # 只获取一条数据
    print(cursor.fetchone())  # 只获取一条数据
    cursor.scroll(2,'absolute')  # 控制光标移动 absolute相对于起始位置,往后移动几位
    cursor.scroll(1,'relative')  # relative相对于当前位置,往后移动几位
    print(cursor.fetchall())  # 获取所有的数据,返回的结果是一个列表
    
    cursor.close()
    conn.close()
    

    八、sql注入问题

    import pymysql
    
    
    conn = pymysql.connect(
        host = '127.0.0.1',
        port = 3306,
        user = 'root',
        password = '123',
        database = 'day38',
        charset = 'utf8',  # 编码千万不要加- 如果写成了utf-8会直接报错
        autocommit = True  # 这个参数配置完成后  增删改操作都不需要在手动加conn.commit了
    )
    cursor = conn.cursor(pymysql.cursors.DictCursor)  # 产生一个游标对象  以字典的形式返回查询出来的数据 键是表的字段  值是表的字段对应的信息
    
    
    
    # sql = 'insert into user(name,password) values("jerry","666")'
    # sql = 'update user set name = "jasonhs" where id = 1'
    sql = 'delete from user where id = 6'
    cursor.execute(sql)
    
    
    """
    增删改操作 都必须加一句
    conn.commit()操作
    """
    # conn.commit()
    # username = input('username>>>:')
    # password = input('password>>>:')
    # sql = "select * from user where name =%s and password = %s"
    # print(sql)
    # res = cursor.execute(sql,(username,password))  # 能够帮你自动过滤特殊符号 避免sql注入的问题
    # # execute 能够自动识别sql语句中的%s 帮你做替换
    # if res:
    #     print(cursor.fetchall())
    # else:
    #     print('用户名或密码错误')
    
    
    """
    sql注入 就是利用注释等具有特殊意义的符号 来完成一些骚操作
    后续写sql语句  不要手动拼接关键性的数据
    而是让excute帮你去做拼接
    """
    
    # 不要手动去拼接查询的sql语句
    username = input(">>>:").strip()
    password = input(">>>:").strip()
    sql = "select * from user where username='%s' and password='%s'"%(username,password)
    
    # 用户名正确
    username >>>: jason' -- jjsakfjjdkjjkjs
    # 用户名密码都不对的情况
    username >>>: xxx' or 1=1 --asdjkdklqwjdjkjasdljad
    password >>>: ''
    

    九、增删改

    # 增
    sql = "insert into user(username,password) values(%s,%s)"
    rows = cursor.excute(sql,('jason','123'))
    
    # 修改
    sql = "update user set username='jasonDSB' where id=1"
    rows = cursor.excute(sql)
    
    """
    增和改单单执行excute并不会真正影响到数据,需要再执行conn.commit()才可以完成真正的增改
    """
    
    # 一次插入多行记录
    res = cursor,excutemany(sql,[(),(),()]
    
  • 相关阅读:
    WebRTC相关技术预研总结
    What is "jar.mn"?
    (FFOS Gecko & Gaia) OTA
    (FFOS Gecko & Gaia) OTA
    (FFOS Gecko & Gaia) OTA
    (FFOS Gecko & Gaia) OTA
    (FFOS Gecko & Gaia) OTA
    (FFOS Gecko & Gaia) OTA
    (FFOS Gecko & Gaia) OTA
    EF实体框架 5 性能注意事项
  • 原文地址:https://www.cnblogs.com/DcentMan/p/11402400.html
Copyright © 2011-2022 走看看