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模块的更多相关文章

  1. python开发mysql:Pymysql模块

    pymysql模块的使用 #1 基本使用 # import pymysql # conn=pymysql.connect(host='localhost',user='root',password=' ...

  2. python连接MySQL pymysql模块,游标,SQL注入问题,增删改查操作

    pymysql模块 pymysql是用python控制终端对MySQL数据库进行操作的第三方模块 import pymysql # 1.连接数据库 client = pymysql.connect( ...

  3. MySQL— pymysql模块(防止sql注入),可视化软件Navicat

    一.Pymysql import pymysql #python2.X 中是 mysqldb 和 pythonmysql 用法是一模一样的 #pymysql可以伪装成上面这两个模块 user = in ...

  4. day40:python操作mysql:pymysql模块&SQL注入攻击

    目录 part1:用python连接mysql 1.用python连接mysql的基本语法 2.用python 创建&删除表 3.用python操作事务处理 part2:sql注入攻击 1.s ...

  5. 多表查询、可视化工具、pymysql模块

    create table dep( id int primary key auto_increment, name varchar(16), work varchar(16) ); create ta ...

  6. Navicat,SQL注入,pymysql模块

    # 关键字exists(了解) 只返回布尔值 True False 返回True的时候外层查询语句执行 返回False的时候外层查询语句不再执行 select * from emp where exi ...

  7. Python中操作mysql的pymysql模块详解

    Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...

  8. python 存储引擎 mysql(库,表, 行) 单表多表操作 (foreign key) sql_mode pymysql模块讲解

    ##################总结############### mysql 常用数据类型 整型:tinyint  int(42亿条左右)  bigint 小数:float double dec ...

  9. (转)Python中操作mysql的pymysql模块详解

    原文:https://www.cnblogs.com/wt11/p/6141225.html https://shockerli.net/post/python3-pymysql/----Python ...

  10. python 全栈开发,Day63(子查询,MySQl创建用户和授权,可视化工具Navicat的使用,pymysql模块的使用)

    昨日内容回顾 外键的变种三种关系: 多对一: 左表的多 对右表一 成立 左边的一 对右表多 不成立 foreign key(从表的id) refreences 主表的(id) 多对多 建立第三张表(f ...

随机推荐

  1. Oracle EBS 更新客户地点

    --更新客户地点 declare x_return_status ); x_msg_count NUMBER; x_msg_data ); x_profile_id NUMBER; l_locatio ...

  2. 用NSOperation写下载队列

    用NSOperation写下载队列 说明 1. 支持缓存机制 2. 图片都是在主线程中加载 3. 文件名用了md5加密 *这东西被人写烂了,但大伙如果对NSOperation不熟悉的话,可以看看本人的 ...

  3. 生活随记[All]

    1. 心灵鸡汤[all] 2. 工作总结 [all] 3. 面试经验[all] 4. 其他

  4. 一、Linux中的常用命令2 二、Vim编辑器的使用

    一.Linux的常用命令###<1>文件目录操作 13. echo:用于输出字符串,shell编程,echo 1. 输出字符串 : echo str ,shell编程会使用(类似java中 ...

  5. php实现随机数字、字母的验证码

     php实现随机数字.字母的验证码 可自定义生成验证码文字的大小.数量.干扰项等等,也可以自定义验证文字的字体... 废话不多说,直接上代码: 1.classgd.class.php <?php ...

  6. css3动画效果小结

    css3的动画功能有以下三种: 1.transition(过度属性) 2.animation(动画属性) 3.transform(2D/3D转换属性) 下面逐一进行介绍我的理解: 1.transiti ...

  7. SDWC补题计划

    2018的寒假去了SD的冬令营,因为一班二班难度悬殊,对我很不友好,几乎什么也没学会,但是我把两个班的课件都存了下来,现在慢慢把两个班的例题以及课后题都补一补(毕竟冬令营的钱不能白花). 这些题目横跨 ...

  8. sql按月模糊查询

    select * from tb where convert(varchar(7),date,120) = '2011-05'

  9. springmvc IDEA

    回顾Java平台上Web开发历程来看,从Servlet出现开始,到JSP繁盛一时,然后是Servlet+JSP时代,最后演化为现在Web开发框架盛行的时代.一般接触到一个新的Web框架,都会想问这个框 ...

  10. 分布式缓存技术redis系列(一)——redis简介以及linux上的安装

    redis简介 redis是NoSQL(No Only SQL,非关系型数据库)的一种,NoSQL是以Key-Value的形式存储数据.当前主流的分布式缓存技术有redis,memcached,ssd ...