day40:python操作mysql:pymysql模块&SQL注入攻击
目录
part1:用python连接mysql
1.用python连接mysql的基本语法
创建连接conn→创建游标对象cursor→执行sql语句execute→获取数据fetchone→释放游标对象cursor→关闭连接conn
# (1) 创建连接 host user password database 这四个参数必须写
conn = pymysql.connect(host="127.0.0.1",user="root",password="",database="db0826",charset="utf8",port=3306) # (2) 创建游标对象(该对象可以操作数据库增删改查)
cursor = conn.cursor() # (3) 执行sql语句
sql = "select * from employee"
# 返回的是数据的总条数
res = cursor.execute(sql)
print(res) # (4)获取数据 fetchone 获取一条
res = cursor.fetchone()
res = cursor.fetchone()
print(res) # (5) 释放游标对象
cursor.close() # (6) 关闭连接
conn.close()
2.用python 创建&删除表
1,2,5,6步骤都是不变的,只改变3.4步即可
conn = pymysql.connect(host="127.0.0.1",user="root",password="",database="db0826")
cursor = conn.cursor() # 1.创建一张表
sql = """
create table t1(
id int unsigned primary key auto_increment,
first_name char(10) not null,
last_name char(10) not null,
age int unsigned,
sex tinyint,
money float
)
""" # res = cursor.execute(sql)
# print(res) # 2.查看表结构
"""
sql = "desc t1"
res = cursor.execute(sql)
print(res) # 6条字段数据
print(cursor.fetchone())
print(cursor.fetchone())
print(cursor.fetchone())
print(cursor.fetchone())
print(cursor.fetchone())
print(cursor.fetchone())
""" # 3.删除表
"""
try:
sql = "drop table t1"
res = cursor.execute(sql)
print(res)
except:
pass
"""
cursor.close()
conn.close()
3.用python操作事务处理
pymysql 操作事务处理时,需要commit提交数据,才会变化,否则rollback回滚.恢复到最初状态
请注意:你sql语句里写的是增删改,你得到的execute的返回值没有任何意义,所以你fetchone是无效的,只有你sql语句写的是查询的操作,fetchone才能获取到数据
conn = pymysql.connect(host="127.0.0.1",user="root",password="",database="db0826")
cursor = conn.cursor()
sql1 = "begin"
sql2 = "update employee set emp_name = '123egon' where id = 1"
sql3 = "commit"
res1 = cursor.execute(sql1)
res2 = cursor.execute(sql2)
res3 = cursor.execute(sql3)
# print(res1,res2,res3) # 返回值没有意义
# fetchone 与查询sql有关 , 增删改无效;
# tup = cursor.fetchone()
# print(tup) cursor.close()
conn.close()
part2:sql注入攻击
先创建一张用户名-密码表
create table usr_pwd(
id int unsigned primary key auto_increment,
username varchar(255) not null,
password varchar(255) not null
)
1.sql注入的现象
import pymysql
user = input("请输入用户名: >>> ").strip()
pwd = input("请输入密码: >>> ").strip() conn = pymysql.connect(host="127.0.0.1",user="root",password="",database="db0826")
cursor = conn.cursor() sql = "select * from usr_pwd where username='%s' and password='%s' " % (user,pwd)
print(sql)
res = cursor.execute(sql)
print(res) # 查询的条数 if res:
print("登录成功")
else:
print("登陆失败") cursor.close()
conn.close()
输入时账号输入:sfsdf' or 3=3 -- sdfsd 密码随意输入都可以登录成功
原因:-- 后面的字符串都会被注释掉, 前面账号虽然是错的 但是 2=2是真的 绕开了账号和密码的判断;
select * from usr_pwd where username='afasdfasdfasdf' or 2=2 -- sfasdf' and password='3434
2.防止sql注入:使用预处理,提前对sql语句中的特殊符号进行处理
使用预处理机制,可以避免绝大多数sql注入的问题
execute 如果参数为2个,将默认开启预处理
execute(sql , (参数1,参数2,参数3 .... ) )
import pymysql
user = input("请输入用户名: >>> ").strip()
pwd = input("请输入密码: >>> ").strip() conn = pymysql.connect(host="127.0.0.1",user="root",password="",database="db0826")
cursor = conn.cursor()
sql = "select * from usr_pwd where username=%s and password=%s"
res = cursor.execute(sql, (user,pwd) ) print("登陆成功" if res else "登录失败") cursor.close()
conn.close()
part3:python操作mysql增删改查
1.创建游标时,可以指定返回值类型为其他(默认是元组)
# 创建mysql 链接
conn = pymysql.connect(host="127.0.0.1",user="root",password="",database="db0826") # 查询数据,默认是元组,可以设置返回的类型为字典 pymysql.cursors.DictCursor
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
2.python操作mysql增操作
cursor.executemany:可以一次插入多条数据
cursor.lastrowid:获取最后插入这条数据的id号(仅针对单条数据插入)
# sql增语句
sql = "insert into t1(first_name,last_name,age,sex,money) values(%s,%s,%s,%s,%s)" # 一次插入一条
res = cursor.execute(sql, ("宋","云杰",30,0,15000) )
print(res) #
# 获取最后插入这条数据的id号(针对单条数据插入)
print(cursor.lastrowid) # # 一次插入多条
res = cursor.executemany( sql, [ ("高","云峰",50,1,16000) , ("戈","隆",80,1,17000) , ("袁","伟倬",120,0,130000) , ("刘","欣慰",150,0,18000) ] )
print(res) # 打印的是插入的条数4 # 针对于多条数据,搜最后的id 可以通过倒序查询id
sql = "select id from t1 order by id desc limit 1"
res = cursor.execute(sql)
print(res)
# 获取最后一个id号
res = cursor.fetchone()
print(res)
3.python操作mysql删操作
sql = "delete from t1 where id = %s"
res = cursor.execute(sql , (3,))
print(res)
if res:
print("删除成功")
else:
print("删除失败")
4.python操作mysql改操作
sql = "update t1 set first_name = %s where id = %s"
res = cursor.execute(sql,("王",4))
print(res) if res:
print("修改成功")
else:
print("修改失败")
5.python操作mysql查操作
要注意:fetchone fetchmany fetchall 都是基于上一条数据往下查询
1.获取一条数据:fetchone
sql = "select * from t1"
res = cursor.execute(sql)
print(res) # 总条数 res = cursor.fetchone()
print(res) # 获取一条
2.获取多条数据:fetchmany
sql = "select * from t1"
res = cursor.execute(sql)
print(res) # 总条数 data = cursor.fetchmany() # 括号里不写参数,默认搜索的的是一条数据
print(data)
data = cursor.fetchmany(3) # 基于上一次获取完的位置再获取三条
print(data)
data一共是三条数据,我们可以通过for循环取出每一条数据,并且按照自己的格式进行拼接
for row in data :
# print(row)
first_name = row["first_name"]
last_name = row["last_name"]
age = row["age"]
if row["sex"] == 0:
sex = "女性"
else:
sex = "男性" money = row["money"]
print("姓:{},名:{},年龄:{},姓名:{},收入:{}".format(first_name,last_name,age,sex,money) )
3.获取所有数据: fetchall
sql = "select * from t1"
res = cursor.execute(sql)
print(res) # 总条数 data = cursor.fetchall()
print(data)
4.自定义搜索查询的位置
1.相对滚动 (正数相对于当前位置往后滚,负数相对于当前位置往后滚.)
# 向前滚3个
cursor.scroll(3,mode="relative")
res = cursor.fetchone()
print(res) # 往后滚2个
cursor.scroll(-2,mode="relative")
res = cursor.fetchone()
print(res)
2.绝对滚动 , 永远基于第一条数据的位置进行移动
cursor.scroll(0,mode="absolute")
print(cursor.fetchone()) cursor.scroll(1,mode="absolute")
print(cursor.fetchone()) cursor.scroll(3,mode="absolute")
print(cursor.fetchone()) # 往前滚没有数据,超出范围 error报错
cursor.scroll(-1,mode="absolute")
print(cursor.fetchone()) # 在进行增删改查时,必须提交数据,才会产生影响.
conn.commit()
cursor.close()
conn.close()
part4:导出导入数据库
1.导出数据库
第一步: 先退出数据库
第二步: 切换到对应的路径(你想将导出的数据库文件放到哪个目录)
mysqldump -uroot -p db0824 > db0824.sql
第三步:导出所有内容(整个数据库)
mysqldump -uroot -p db0824 > db0824.sql
导出数据库中的单个表
mysqldump -uroot -p db0824 t1 > t1.sql
2.导入数据库
第一步 : 先创建一个空的数据库
第二步 : 找到sql对应文件目录
第三步 : source 路径/文件
use 数据库
source D:\db0824.sql
day40:python操作mysql:pymysql模块&SQL注入攻击的更多相关文章
- python之MySQL学习——防止SQL注入
python之MySQL学习——防止SQL注入 学习了:https://www.cnblogs.com/xiaomingzaixian/p/7126840.html https://www.cnblo ...
- Python操作mysql之模块pymysql
pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文环境 python3.6.1 Mysql ...
- mysql数据库----python操作mysql ------pymysql和SQLAchemy
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy 一.pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQ ...
- python连接MySQL pymysql模块,游标,SQL注入问题,增删改查操作
pymysql模块 pymysql是用python控制终端对MySQL数据库进行操作的第三方模块 import pymysql # 1.连接数据库 client = pymysql.connect( ...
- MySQL多表查询,Navicat使用,pymysql模块,sql注入问题
一.多表查询 #建表 create table dep( id int, name varchar(20) ); create table emp( id int primary key auto_i ...
- Navicat工具、pymysql模块 sql注入
cls超 Navicat工具.pymysql模块 阅读目录 一 IDE工具介绍 二 pymysql模块 一 IDE工具介绍(Navicat) 生产环境还是推荐使用mysql命令行,但为了方便我们测试, ...
- python开发mysql:Pymysql模块
pymysql模块的使用 #1 基本使用 # import pymysql # conn=pymysql.connect(host='localhost',user='root',password=' ...
- Python操作Mysql数据库时SQL语句的格式问题
一.概述 近日使用Python对Mysql数据库进行操作,遇到SQL语句死活出问题的情况.由于最初没有将异常打印出来,一直不知道原因.随后,将异常打印出来之后,通过异常信息,对代码进行修改.最终,成功 ...
- python之MySQL学习——防止SQL注入(参数化处理)
import pymysql as ps # 打开数据库连接 db = ps.connect(host=', database='test', charset='utf8') # 创建一个游标对象 c ...
随机推荐
- PHP is_infinite() 函数
------------恢复内容开始------------ 实例 判断一个值是否为无限值: <?php echo is_infinite(2) . "<br>" ...
- PHP time_nanosleep() 函数
实例 延迟执行当前脚本 3,5 秒: <?phpif (time_nanosleep(3,500000000) === true){高佣联盟 www.cgewang.comecho " ...
- PHP quoted_printable_decode() 函数
实例 对经过 quoted-printable 编码后的字符串进行解码,返回 8 位的 ASCII 字符串: <?php高佣联盟 www.cgewang.com$str = "Hell ...
- layui实现图片上传
页面代码: <style> .uploadImgBtn2{ width: 120px; height: 92px; cursor: pointer; position: relative; ...
- 一个轻量级的基于RateLimiter的分布式限流实现
上篇文章(限流算法与Guava RateLimiter解析)对常用的限流算法及Google Guava基于令牌桶算法的实现RateLimiter进行了介绍.RateLimiter通过线程锁控制同步,只 ...
- CI4框架应用五 - 加载视图
这节我们来看一下CI4框架中视图的加载, CI4中提供了几种方式加载视图. 1. 利用CI4框架提供的全局函数view(‘模板名’),如果需要传参数可以在第二个参数传一个数组 我们先修改一下之前定义的 ...
- 037_go语言中的互斥锁
代码演示: package main import ( "fmt" "math/rand" "runtime" "sync&quo ...
- 学生成绩管理系统-JAVA语言测试
首先右键新建一个工程project 选择Java Project,单击next下一步 project命名为“学生成绩管理系统”,点击finish继续 右键src文件夹新建Package包,取名为te ...
- 用 Python 写个坦克大战
坦克大战是一款策略类的平面射击游戏,于 1985 年由 Namco 游戏公司发布,尽管时至今日已经有了很多衍生类的游戏,但这款游戏仍然受到了相当一部分人的欢迎,本文我们看一下如何使用 Python 来 ...
- 【系统之音】WindowManager工作机制详解
前言 目光所及,皆有Window!Window,顾名思义,窗口,它是应用与用户交互的一个窗口,我们所见到视图,都对应着一个Window.比如屏幕上方的状态栏.下方的导航栏.按音量键调出来音量控制栏.充 ...