一、子查询   

子查询:把一个查询语句用括号括起来,当做另外一条查询语句的条件去用,称为子查询

select emp.name from emp inner join dep on emp.dep_id = dep.id where dep.name="技术";

select name from emp where dep_id =
(select id from dep where name="技术");

查询平均年龄在25岁以上的部门名
select name from dep where id in
(select dep_id from emp group by dep_id having avg(age) > 25);

select dep.name from emp inner join dep on emp.dep_id = dep.id
group by dep.name
having avg(age) > 25;

查看不足2人的部门名(子查询得到的是有人的部门id)

   

  补:exists存在

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

查询每个部门最新入职的那位员工

select t1.id,t1.name,t1.post,t1.hire_date,t2.post,t2.max_date from emp as t1 inner join
(select post,max(hire_date) as max_date from emp group by post) as t2
on t1.post = t2.post
where t1.hire_date = t2.max_date
;

二、pymysql模块

基本操作之查询:  

import pymysql #pip3 install pymysql

conn=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='db42',
charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor) #查询结果以字典形式显示 sql='select * from class;'
rows=cursor.execute(sql) #执行一条sql语句,返回值是执行结果的行数
print(rows) print(cursor.fetchone()) #取出一行执行结果,光标向后移动一位
print(cursor.fetchone()) #取出第二行执行结果 光标向后移动一位
print(cursor.fetchmany(2)) #取出两行执行结果 光标向后移动两位
print(cursor.fetchall()) #取出剩余的全部
print(cursor.fetchall()) # print(cursor.fetchall())
# cursor.scroll(3,'absolute') # 绝对路径 光标从最开始的位置开始向后移动3个位置
# print(cursor.fetchone()) #取出当前光标所在位置的数据,并将光标后移 print(cursor.fetchone())
print(cursor.fetchone())
cursor.scroll(1,'relative') #相对路径 光标从当前位置开始向后移动3个位置
print(cursor.fetchone()) conn.commint() #提交事务
cursor.close()
conn.close()

  基本操作之增改:

import pymysql #pip3 install pymysql

conn=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='db42',
charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor) #一次插入一行记录
sql='insert into user(username,password) values(%s,%s)'
rows=cursor.execute(sql,('EGON','123456'))
print(rows)
print(cursor.lastrowid) rows=cursor.execute('update user set username="alexSB" where id=2')
print(rows) # 一次插入多行记录
sql='insert into user(username,password) values(%s,%s)'
rows=cursor.executemany(sql,[('lwz','123'),('evia','455'),('lsd','333')])
print(rows)
print(cursor.lastrowid) conn.commit() # 只有commit提交才会完成真正的修改
cursor.close()
conn.close()

三、防止sql注入问题 

1、当输入 正确的用户名 并在其后加上 '--' sql注释符时, 我们执行这条sql 后边的密码验证判断就被注释掉了,从而没有密码就可以登录

2、 或者 在用户输入用户名时,随意输入用户名 并加上 'or 1=1' 这种语句,则跳过了账户密码的验证直接登陆进去了

所以rows=cursor.execute(sql)这种拼接sql ,直接执行的方法并不可靠   

import pymysql #pip3 install pymysql

conn=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='db42',
charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor) inp_user=input('用户名>>:').strip() #inp_user=""
inp_pwd=input('密码>>:').strip() #inp_pwd=""
sql="select * from user where username='%s' and password='%s'" %(inp_user,inp_pwd) #1、当输入 正确的用户名 并在其后加上 '--' sql注释符时, 我们执行这条sql 后边的密码验证判断就被注释掉了,从而没有密码就可以登录
#2、 或者 在用户输入用户名时,随意输入用户名 并加上 'or 1=1' 这种语句,则跳过了账户密码的验证直接登陆进去了 rows=cursor.execute(sql) #所以这种拼接sql ,直接执行的方法并不可靠
if rows:
print('登录成功')
else:
print('登录失败') cursor.close()
conn.close()

  解决办法:

pymysql 为我们提供了过滤字符串中特殊符号的功能

  使用如下方法传入参数,会自动帮我们过滤掉特殊字符,防止sql注入  

inp_user=input('用户名>>:').strip() #inp_user=""
inp_pwd=input('密码>>:').strip() #inp_pwd=""
sql="select * from user where username=%s and password=%s" rows=cursor.execute(sql,(inp_user,inp_pwd))

5月11日 python学习总结 子查询、pymysql模块增删改查、防止sql注入问题的更多相关文章

  1. 4月11日 python学习总结 对象与类

    1.类的定义 #类的定义 class 类名: 属性='xxx' def __init__(self): self.name='enon' self.age=18 def other_func: pas ...

  2. 6月11日 python学习总结 框架理论

    Web框架本质及第一个Django实例   Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web ...

  3. 4月19日 python学习总结 套接字模块的使用

    服务端: import socket phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 买电话 phone.bind(('127.0.0 ...

  4. python学习之列表的定义以及增删改查

    列表定义: >>> name['lily','lucy','tom'] >>> nums = [11,22,33,'100','lily'] #python中的列表 ...

  5. mysql python pymysql模块 增删改查 插入数据 介绍 commit() execute() executemany() 函数

    import pymysql mysql_host = '192.168.0.106' port = 3306 mysql_user = 'root' mysql_pwd = ' encoding = ...

  6. mysql python pymysql模块 增删改查 查询 fetchone

    import pymysql mysql_host = '192.168.0.106' port = 3306 mysql_user = 'root' mysql_pwd = ' encoding = ...

  7. mysql python pymysql模块 增删改查 查询 字典游标显示

    我们看到取得结果是一个元祖,但是不知道是哪个字段的,如果字段多的时候,就比较麻烦 ''' (1, 'mike', '123') (2, 'jack', '456') ''' 用字典显示查询的结果,也可 ...

  8. mysql python pymysql模块 增删改查 查询 fetchmany fetchall函数

    查询的fetchmany fetchall函数 import pymysql mysql_host = '192.168.0.106' port = 3306 mysql_user = 'root' ...

  9. python链接oracle数据库以及数据库的增删改查实例

    初次使用python链接oracle,所以想记录下我遇到的问题,便于向我这样初次尝试的朋友能够快速的配置好环境进入开发环节. 1.首先,python链接oracle数据库需要配置好环境. 我的相关环境 ...

随机推荐

  1. Solution Set -「LOCAL」冲刺省选 Round XXIII

    \(\mathscr{Summary}\)   有一说一,虽然我炸了,但这场锻炼心态的效果真的好.部分分聊胜于无,区分度一题制胜,可谓针对性强的好题.   A 题,相对性签到题.这个建图确实巧妙,多见 ...

  2. Solution -「多校联训」种蘑菇

    \(\mathcal{Description}\)   Link.   给定一棵含有 \(n\) 个结点的树,设 \(S\) 为其中的非空联通子集,求 \[\sum_{S}(\gcd_{u\in S} ...

  3. Windows原理深入学习系列-强制完整性控制

    欢迎关注微信公众号:[信安成长计划] 0x00 目录 0x01 介绍 0x02 完整性等级 0x03 文件读取测试 0x04 进程注入测试 0x05 原理分析 Win10_x64_20H2 0x06 ...

  4. 渗透测试工程师认证 | CISP-PTE证书含金量

    注册渗透测试工程师(CISP-PTE)认证是由中国信息安全测评中心针对攻防专业领域实施的资质培训, 是国内唯一针对网络安全渗透测试专业人才的资格认证,是目前国内最为主流及被业界认可的专业攻防领域的资质 ...

  5. eBPF会成为服务网格的未来吗?

    服务网格现状 服务网格为服务提供了复杂的应用层网络管理,如服务发现.流量路由.弹性(超时/重试/断路).认证/授权.可观察性(日志/度量/追踪)等. 在分布式应用的早期,这些要求是通过直接将所需的逻辑 ...

  6. 赶紧收藏!最好用的BI工具都在这了!

    1.bi厂商--思迈特软件Smartbi 广州思迈特软件有限公司成立于2011 年,以提升和挖掘企业客户的数据价值为使命,专注于商业智能与大数据分析软件产品与服务.思迈特软件是国家认定的"高 ...

  7. BI企服界大众点评来袭!Smartbi入围36氪企服软件系列三大榜单!

    近日,36氪企服点评中国商业智能BI金榜揭晓.作为国产民族BI软件的领跑者,思迈特软件凭借深耕多年大数据BI领域中拥有过硬的产品实力与优质的服务,荣获"商业智能BI最佳软件总榜TOP10&q ...

  8. 【C# 程序集】.NET core Could not load file or assembly

    NET core 添加了新的nuget包,部署出现Could not load file or assembly 这个坑,今天整了一天,我添加了Microsoft.AspNetCore.Mvc.Ver ...

  9. PromQL全解析

    PromQL(Prometheus Query Language)为Prometheus tsdb的查询语言.是结合grafana进行数据展示和告警规则的配置的关键部分. 本文默认您已了解Promet ...

  10. while + else 使用,while死循环与while的嵌套,for循环基本使用,range关键字,for的循环补充(break、continue、else) ,for循环的嵌套,基本数据类型及内置方法

    今日内容 内容概要 while + else 使用 while死循环与while的嵌套 for循环基本使用 range关键字 for的循环补充(break.continue.else) for循环的嵌 ...