pymysql的操作
python 操作mysql
安装pymysql 模块
pip install pymysql
sql注入问题
输入用户名:qaa ' or 1=1 #
输入密码:dasdasdsa
select * from user where name='qaa' or 1=1 #' and password='afasa'
不需要输入正确用户名和密码就能查看数据
产生的原因
因为过于相信用户输入的内容,根本没有做任何的检验
解决的方法
sql = 'select * from user where name=%s and password=%s'
cursor.execute(sql,(user,pwd))
用cursor.execute来检验输入的用户名和密码
连接
连接数据库的参数
conn = pymysql.connect(host='localhost',user ='root',passwrod ='123',database='test',charset='utf8')
cursor=conn.cursor() 默认返回的值是元祖类型
cursor =conn.cursor(cursor=pymysql.cursors.DictCursor) 返回的是字典类型
查
fetchall(): 取出所有的数据,返回的是列表套字典
fetchone():取出一条数据 返回的是字典
fetchmany(size):取出size条数据 返回的是列表套字典
增
sql= "inset into user(name,password)values (%s,%s)"
cursor.execute(sql,('xxx','qwe')) # 新增一条数据
data=[
('aaa','qqq')
('aaa1','qqq1')
('aaa2','qqq2')
]
cursor.executemany(sql,data) # 新增多条数据
conn.commit() # 除了查 ,必须要加该代码
print(cursor.lastrowid)# 获取最后一行的id
修
sql="update user set name=%s where id=%s"
cursor.execute(sql,('ghh',2))
conn.commit()
cursor.close()
conn.close()
删
sql ="delete from user where id =%s"
cursor.execute(sql,('sdas')
conn.commit()
cursor.close()
conn.close()
索引
使用索引的作用
为了提高查询的效率
类比:字典中的目录
索引的本质
一个特殊的文件
索引的底层原理
B+树
索引的种类
主键索引:加速查找+不能重复+不能为空 primary key
唯一索引:加速查找+不能重复 unique(name)
联合唯一索引: unique(name,email)
普通索引: 加速查找 index(name)
联合索引:index ( name,email)
索引的创建
主键索引
- 新增主键索引:
create table xxx(
id int auto_increment,
primary key(id)
)
alter table xxx change id id int auto_increment primary key
alter table xxx add primary key(id);
- 删除主键索引:
alter table xxx drop primary key;
唯一索引
- 新增:
create table x1(
id int auto_increment primary key,
name varchar(32) not null default '',
unique u_name(name)
)charset utf8
create unique index 索引名 on 表名(字段名);
create unique index ix_name on x1(name);
alter table x1 add unique index ix_name(name)
- 删除:
alter table x1 drop index u_name;
普通索引
- 新增:
create table x2(
id int auto_increment primary key,
name varchar(32) not null default '',
index u_name(name)
)charset utf8
create index 索引名 on 表名(字段名);
create index ix_name on x2(name);
alter table x2 add index ix)name(name)
- 删除:
alter table x2 drop index u_name;
索引的优缺点
通过观察*.ibd文件可知:
- 索引加快了查询速度
- 占用大量的磁盘空间
不会命中索引的情况
不能再SQL语句中,进行四则运算,会降低SQL的查询效率
使用函数
select * from tb1 where reverse(email) ='aaa';
类型不一致
如果列是字符串类型,传入的条件必须用引号引起来
排序条件为索引,则select字段必须也是索引字段,否则无法命中
order by
select name from s1 order by email desc;
当根据索引排序时候,select查询的字段如果不是索引,则速度仍然很慢
select email from s1 order by email desc;
特别的:如果对主键排序,速度还是很快
select * from tb1 order by nid desc;
count(1)或count(列)代替count(*)在mysql中没有差别
组合索引最左前缀
什么时候回创建联合索引?
根据公司的业务场景,在嘴常用的几列上添加索引
select * from user where name='mqb' and email ='mqb@qq.com';
如果遇到上述业务情况,错误的做法是:
index ix_name(name),
index ix_email(email)
正确的做法:
index ix_name_email(name,email)
如果组合索引为:ix_name_email (name,email)
where name='zekai' and email='xxxx' -- 命中索引
where name='zekai' -- 命中索引
where email='zekai@qq.com' -- 未命中索引explain
explain select * from user where name ='mqb' and email ='mqb @qq.com'\G
id: 1
select_type: SIMPLE
table: user
partitions: NULL
type: ref 索引指向 all
possible_keys: ix_name_email 可能用到的索引
key: ix_name_email 确实用到的索引
key_len: 214 索引长度
ref: const,const
rows: 1 扫描的长度
filtered: 100.00
Extra: Using index 使用到了索引
索引覆盖:
select id from user where id =2000;
慢查询日志
查看慢SQL的相关变量
show variables like '%slow%'
配置慢SQL的变量
set global 变量名=值
set global slow_query_log=on;
set global slow_query_log_file='地址';
set global long_query_time=1;
pymysql的操作的更多相关文章
- 使用with语句优化pymysql的操作
一.with语句的好处 with语句的好处在于,它可以自动帮我们释放上下文,就比如文件句柄的操作,如果你不使用with语句操作,你要先open一个文件句柄,使用完毕后要close这个文件句柄,而使用w ...
- 第二百七十九节,MySQL数据库-pymysql模块操作数据库
MySQL数据库-pymysql模块操作数据库 pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数使用方式: 模块名称.connec ...
- pymysql 简单操作数据库
#!/usr/bin/env python #-*- coding:utf-8 -*- # author:leo # datetime:2019/4/24 15:22 # software: PyCh ...
- python学习笔记(15)pymysql数据库操作
pymysql数据库操作 1.什么是PyMySQL 为了使python连接上数据库,你需要一个驱动,这个驱动是用于与数据库交互的库. PyMySQL : 这是一个使Python连接到MySQL的库,它 ...
- MySQL数据库-pymysql模块操作数据库
pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数使用方式: 模块名称.connect() 参数: host=数据库ip port= ...
- pymysql模块操作数据库
pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数 使用方式: 模块名称.connect() 参数: host=数据库ip po ...
- python 通过 pymysql模块 操作 mysql 数据库
Python 中操作 MySQL 步骤 安装模块 pip install pymysql 引入模块 在py文件中引入pymysql模块 from pymysql import * Connection ...
- pymysql模块操作数据库及连接报错解决方法
import pymysql sql = "select host,user,password from user" #想要执行的MySQL语句 #sql = 'create da ...
- 利用PyMySQL模块操作数据库
连接到数据库 import pymysql # 创建链接得到一个链接对象 conn = pymysql.Connect( host="127.0.0.1", # 数据库服务器主机地 ...
随机推荐
- Java 9 ← 2017,2019 Java → 13 ,都发生了什么?
距离 2019 年结束,只剩下 35 天了.你做好准备迎接 2020 年了吗? 一到年底,人就特别容易陷入回忆和比较之中,比如说这几天, 的对比挑战就火了! 这个话题登上了微博的热搜榜,也刷爆了朋友圈 ...
- python-面向对象之封装
封装 面向对象三大特性: 继承 封装 多态 隐藏对象的属性和实现细节,仅对外提供公共访问方法 广义上的封装 : 把方法和变量都封装在类中 狭义上的封装 : 在类的外部干脆不能调用了 优点 将变化隔离 ...
- 阿里架构师花近十年时间整理出来的Java核心知识pdf(Java岗)
由于细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容! 整理了一份Java核心知识点.覆盖了JVM.锁.并发.Java反射.Spring原理.微服务.Zooke ...
- PHP如何获取视频总时长与码率等信息
利用PHP中的FFmpeg读取视频播放时长与码率等信息 function getVideoInfo($file) { define('FFMPEG_PATH', '/usr/local/ff ...
- Day01-初识 Python
1.CPU/内存/硬盘/操作系统 CPU :计算机的运算和处理中心,相当于人类的大脑. 内存 :暂时存储数据,临时加载数据应用程序. 硬盘 :长期存储数据. 操作系统:一个软件,连接计算机的硬件与所有 ...
- NSAttributedString可以强制转换为NSMutableAttributedString类型吗?下面这代码有什么问题 为什么报错
-(void)insetEmotion:(EmotionModel*)emotionModel{ if(emotionModel.code){ /** 在TextView中插入图片首选要知道光标的位置 ...
- 阿里巴巴的 Kubernetes 应用管理实践经验与教训
作者 | 孙健波(天元) 阿里巴巴技术专家 导读:本文整理自孙健波在 ArchSummit 大会 2019 北京站演讲稿记录.首先介绍了阿里巴巴基于 Kubernetes 项目进行大规模应用实践过程 ...
- Java修炼——递归算法的俩个实例
1.是输出指定文件目录下的所以子目录以及文件 2.使用递归算算法:1!+2!+3!+4!+5!+-+n!(计算阶乘累加) package com.bjsxt.recurison; import jav ...
- ASE19团队项目beta阶段Backend组 scrum6 记录
本次会议于12月12日,19:30在微软北京西二号楼sky garden召开,持续10分钟. 与会人员:Zhikai Chen, Lihao Ran, Xin Kang 请假人员:Hao Wang 每 ...
- HDU3247 Resource Archiver (AC自动机+spfa+状压DP)
Great! Your new software is almost finished! The only thing left to do is archiving all your n resou ...