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,[(),(),()]
最新文章
- 工作中最常用的Excel函数公式大全
- UEditor 1.4.3.1.NET版本上传配置备忘录
- lucene 区分大小写 问题以及解决方案
- js 中文排序
- PHP5.4的变化关注---What has changed in PHP 5.4.x(转)
- 【c语言】推断一个数是不是2的n次方
- winform控件记录
- ODPS 下一个map / reduce 准备
- 循序渐进看Java web日志跟踪(3)-Log4J的使用和配置
- 封装Jquery 合并table中任何同列数据
- MVC下 把数据库中的byte[]值保存成图片,并显示在view页面
- Scala 隐式(implicit)详解
- 7.2内存管理-ARC
- Spring Cloud 使用 FeignClient 启动报错
- HTML基础之DOM操作
- iOS-AFNetworking参数和多文件同时上传【多文件上传】
- asp.net web api 2 host in a windows service推荐阅读
- Selenium-Grid工作原理
- sourceTree 添加 ssh key 方法【转】
- C# 生成序号不足补0
热门文章
- Codeforces 524C.The Art of Dealing with ATM(暴力)
- laravel 增强代码提示功能插件
- mysql(2):索引
- 题解【洛谷P3662】[USACO17FEB]Why Did the Cow Cross the Road II S
- winform学习(7)Label控件、Button控件、TextBox控件
- Mysql 函数定义及批量数据脚本
- 2019-2020-2 20174314王方正 《网络对抗》 Exp0 Kali安装
- arcgis中的load data加载数据
- JVM探秘:VisualVM监控远程Java进程
- 6_13古代象形符号(UVa1103)<;图的连通块的应用>;