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 ...
随机推荐
- vim和xshell配色
xshell配色: http://www.hookr.cn/xshell-pei-se.html vim配色: 参考该文中的配置方法,包括设置256色等.http://www.cnblogs.com/ ...
- Winform 多个窗口编辑同一条数据同步的实现
场景: 一个主窗口中,可以在列表(DataGridView)里选中一条记录编辑,打开一个编辑窗口(非模态窗口),编辑窗口保存后需要刷新父窗口,由于编辑窗口是非模态窗口,如果打开了多个窗口,并且都是编辑 ...
- 新建一个去除storyboard的项目
新建一个去除storyboard的项目 1. 新建项目并删除 *.storyboard 以及与之相关的杂项 2. 设置 UIWindow 的 rootViewController 复制粘贴代码如下 s ...
- C# 所有特性,特性所在命名空间,那些命名空间拥有特性类
文章持续补充中 特性并不是集中在某一命名空间中,而是不同的特性在不同的命名空间下,特性是某一命名空间下提供的语法糖. 有哪些命名空间提供特性: 命名空间 描述 Microsoft.Build.Fram ...
- 第2次作业——APP案例分析
第一部分 调研, 评测 1.下载软件并使用起来,描述最简单直观的个人第一次上手体验. 知乎,中文互联网最大的知识平台.使用知乎这个APP3年了,目睹了它的兴盛(当然没有衰亡@_@).打开这款APP,主 ...
- Linux上安装ZooKeeper并设置开机启动(CentOS7+ZooKeeper3.4.10)
1下载Zookeeper 2安装启动测试 2.1上载压缩文件并解压 2.2新建 zookeeper配置文件 2.3安装JDK 2.4启动zookeeper 2.5查看zookeeper的状态 3将Zo ...
- MySQL-5.6版本GTID的主从复制
mysql GTID Replication 一.GTID的概述: 1.全局事物标识:global transaction identifieds. 2.GTID事物是全局唯一性的,且一个事务对应一个 ...
- iOS 开源库系列 Aspects核心源码分析---面向切面编程之疯狂的 Aspects
Aspects的源码学习,我学到的有几下几点 Objective-C Runtime 理解OC的消息分发机制 KVO中的指针交换技术 Block 在内存中的数据结构 const 的修饰区别 block ...
- Hadoop学习之路(十一)HDFS的读写详解
HDFS的写操作 <HDFS权威指南>图解HDFS写过程 详细文字说明(术语) 1.使用 HDFS 提供的客户端 Client,向远程的 namenode 发起 RPC 请求 2.name ...
- 【node.js】Stream(流)
Stream 有四种流类型: Readable - 可读操作. Writable - 可写操作. Duplex - 可读可写操作. Transform - 操作被写入数据,然后读出结果. 所有的 St ...