13-[Mysql]--pymysql模块
1、介绍
之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库,那如何在python程序中操作数据库呢?这就用到了pymysql模块,该模块本质就是一个套接字客户端软件,使用前需要事先安装
pip3 install pymysql
import pymysql user = input('user>>>').strip()
pwd = input('pwd>>>').strip() # 建立连接
conn = pymysql.connect(
host='127.0.0.1', # localhost
port=3306,
user='root',
password='root',
db='db10',
charset='utf8'
) # 拿到游标
cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示
#cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) # 以字典显示 # 执行sql语句
sql = "select * from userinfo where username='%s' and password='%s'" % (user, pwd) #注意%s需要加引号
rows = cursor.execute(sql) # 执行sql语句,返回sql查询成功的记录数目 # 关闭游标
cursor.close() # 关闭连接
conn.close() if rows:
print('登录成功', rows)
else:
print('登录失败', rows)
2、execute()之sql注入
(1)原理
注意:符号--会注释掉它之后的sql,正确的语法:--后至少有一个任意字符
根本原理:就根据程序的字符串拼接name='%s',我们输入一个xxx' -- haha,用我们输入的xxx加'在程序中拼接成一个判断条件name='xxx' -- haha'
最后那一个空格,在一条sql语句中如果遇到select * from t1 where id > 3 -- and name='egon';则--之后的条件被注释掉了 #1、sql注入之:用户存在,绕过密码
egon' -- 任意字符 #2、sql注入之:用户不存在,绕过用户与密码
xxx' or 1=1 -- 任意字符
(2)解决方法:
# 原来是我们对sql进行字符串拼接
# sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd)
# print(sql)
# res=cursor.execute(sql) #改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了)
sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上
res=cursor.execute(sql,(user,pwd)) # pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。
3、 增、删、改:conn.commit()
import pymysql
#链接
conn=pymysql.connect(host='localhost',user='root',password='',database='egon')
#游标
cursor=conn.cursor() #part2
sql='insert into userinfo(name,password) values(%s,%s);'
res=cursor.execute(sql,("root","")) # 增加一条数据
print(res) # 执行sql语句,返回sql影响成功的行数 #part3
res=cursor.executemany(sql,[("root",""),("lhf",""),("eee","")]) # 增加多条数据
print(res) conn.commit() # 提交后才发现表中插入记录成功
cursor.close()
conn.close() # in方法的使用
triggerid = ('10011', '10010')
sql = "select * from triggers where triggerid in %s"
ret = handle.execute(sql, triggerid)
print(ret)
4、查:fetchone,fetchmany,fetchall
import pymysql
#链接
conn=pymysql.connect(host='localhost',user='root',password='',database='egon')
#游标
cursor=conn.cursor() #执行sql语句
sql='select * from userinfo;'
rows=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数rows,将结果放入一个集合,等待被查询 res1=cursor.fetchone() # 取出第一条data
res2=cursor.fetchone() # 取出next数据
res3=cursor.fetchone()
res4=cursor.fetchmany(2) # 取出2条数据
res5=cursor.fetchall() # 全部取出来
print(res1)
print(res2)
print(res3)
print(res4)
print(res5)
print('%s rows in set (0.00 sec)' %rows) cursor.close()
conn.close()
5、相对位置,绝对位置,获取插入的最后一条的自增id
import pymysql # 1.建立连接
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='root',
db='db10',
charset='utf8'
) # 2、拿到游标
cursor = conn.cursor(pymysql.cursors.DictCursor) # 以字典形式显示 sql = 'select * from userinfo '
rows = cursor.execute(sql) cursor.scroll(3, mode='absolute') # 绝对位置移动 f.seek
print(cursor.fetchmany(2)) # 指定几条数据 # 5.关闭游标
cursor.close() # 6、关闭连接
conn.close()
import pymysql # 1.建立连接
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='root',
db='db10',
charset='utf8'
) # 2、拿到游标
cursor = conn.cursor(pymysql.cursors.DictCursor) # 以字典形式显示 sql = 'select * from userinfo '
rows = cursor.execute(sql)
print(cursor.fetchone()) # 打印第1条
cursor.scroll(2, mode='relative') # 相对位置移动2个
print(cursor.fetchone()) # 打印第4条 # 5.关闭游标
cursor.close() # 6、关闭连接
conn.close()
import pymysql
conn=pymysql.connect(host='localhost',user='root',password='',database='egon')
cursor=conn.cursor() sql='insert into userinfo(name,password) values("xxx","123");'
rows=cursor.execute(sql)
print(cursor.lastrowid) # 在插入语句后查看 conn.commit() cursor.close()
conn.close()
13-[Mysql]--pymysql模块的更多相关文章
- python开发mysql:Pymysql模块
pymysql模块的使用 #1 基本使用 # import pymysql # conn=pymysql.connect(host='localhost',user='root',password=' ...
- python连接MySQL pymysql模块,游标,SQL注入问题,增删改查操作
pymysql模块 pymysql是用python控制终端对MySQL数据库进行操作的第三方模块 import pymysql # 1.连接数据库 client = pymysql.connect( ...
- MySQL— pymysql模块(防止sql注入),可视化软件Navicat
一.Pymysql import pymysql #python2.X 中是 mysqldb 和 pythonmysql 用法是一模一样的 #pymysql可以伪装成上面这两个模块 user = in ...
- day40:python操作mysql:pymysql模块&SQL注入攻击
目录 part1:用python连接mysql 1.用python连接mysql的基本语法 2.用python 创建&删除表 3.用python操作事务处理 part2:sql注入攻击 1.s ...
- 多表查询、可视化工具、pymysql模块
create table dep( id int primary key auto_increment, name varchar(16), work varchar(16) ); create ta ...
- Navicat,SQL注入,pymysql模块
# 关键字exists(了解) 只返回布尔值 True False 返回True的时候外层查询语句执行 返回False的时候外层查询语句不再执行 select * from emp where exi ...
- Python中操作mysql的pymysql模块详解
Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...
- python 存储引擎 mysql(库,表, 行) 单表多表操作 (foreign key) sql_mode pymysql模块讲解
##################总结############### mysql 常用数据类型 整型:tinyint int(42亿条左右) bigint 小数:float double dec ...
- (转)Python中操作mysql的pymysql模块详解
原文:https://www.cnblogs.com/wt11/p/6141225.html https://shockerli.net/post/python3-pymysql/----Python ...
- python 全栈开发,Day63(子查询,MySQl创建用户和授权,可视化工具Navicat的使用,pymysql模块的使用)
昨日内容回顾 外键的变种三种关系: 多对一: 左表的多 对右表一 成立 左边的一 对右表多 不成立 foreign key(从表的id) refreences 主表的(id) 多对多 建立第三张表(f ...
随机推荐
- 连接AWS Ubuntu服务器
1.在AWS上创建了Ubuntu实例后,在实例里点连接.点使用PuTTY连接,下载PuTTY软件. 2.在所有程序里找到PuTTYgen并打开,点Load选择创建实例时的pem文件,点save pri ...
- SQL SERVER Management Studio编写SQL时没有智能提示的解决方式
1. 检查设置里是否启用智能感知(Intellisence),可以在“工具”→“选项”里设置 2. 如果启用后还是无效,可以新建一个查询窗口查询,输入关键词的前面几个字母看是否有提示(或者使用Ctrl ...
- mysql性能优化-慢查询分析、优化索引和配置 (慢查询日志,explain,profile)
mysql性能优化-慢查询分析.优化索引和配置 (慢查询日志,explain,profile) 一.优化概述 二.查询与索引优化分析 1性能瓶颈定位 Show命令 慢查询日志 explain分析查询 ...
- UIView的无损截图
UIView的无损截图 说明 1. 烂大街的代码 2. 写成category后,方便直接从drawRect中获取绘制出来的图片 3. 可以直接绘制图片供按钮设置背景图片用 4. 无损截图(包括alph ...
- 一个好玩的计算题目(c++ 位运算)
2015/11/10 在一个qq群里面,看到一个好玩的题目.“int foo(int x){return x&-x} foo(2^31-3)这个怎么算 ?” 1.自己也就开始算了: (1) ...
- 生活随记[All]
1. 心灵鸡汤[all] 2. 工作总结 [all] 3. 面试经验[all] 4. 其他
- 多数据源报错 expected single matching bean but found 2: xxx,xxx
问题: expected single matching bean but found 2: xxx,xxx 原因:在 Spring 容器中配置了两个类型Bean,Spring 容器将无法确定到底要用 ...
- Spfa(最短路求解)
spfa(最短路求解) 模板: #include<iostream> #include<cstdio> #include<queue> #include<cs ...
- 用 Visual Studio Code 调试运行在 homestead 环境中的 laravel 程序
由于之前做 .net 开发比较熟悉 visualstudio,所以自 visualstudio code 发布后就一直在不同场合使用 vscode ,比如前端.node等等.最近在做 laravel ...
- 【MySQL学习杂记】 2017年7月13日
1. 关于分组 当select使用groupby语法时,select返回字段集合里面除去 <使用了聚合函数的字段>.<不包含在 group by 子句的字段> 的其他字段,这些 ...