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. 第四章 数据库和SQL 4-3 数据的更新(UPDATE语句的使用方法)

    一.UPDATE语句的基本语法. 二.指定条件的UPDATE语句(搜索型UPDATE) 三.使用NULL进行更新 NULL清空:使用UPDATE可以将列更新为NULL,俗称NULL清空. 四.多列更新 ...

  2. django使用LDAP验证

    1.安装Python-LDAP(python_ldap-2.4.25-cp27-none-win_amd64.whl)pip install python_ldap-2.4.25-cp27-none- ...

  3. linux rz上传文件及出错解决方案

    在把Windows上的文件传至Linux端时用到SecureCRT,一般小文件都没有问题,文件太大时则出现了上传后的文件只有几K大小,当然大于2个G的是不可能传的上去的了.对于几百M到1G多的大文件要 ...

  4. springmvc常用的组件,注解,跳转

    路径映射 XXXHandlerMapping 随开发配置越来越多 注解到java代码中来简化xml配置 请求到哪个Controller 控制器bean Controller 随着开发配置越来越多 注解 ...

  5. November 26th 2016 Week 48th Saturday

    All growth is a leap in the dark. 所有的成长都是黑暗中的一跃. But it is a dark and long night, I can't see any st ...

  6. Android 高级UI设计笔记24:Android 夜间模式之 WebView 实现白天 / 夜间阅读模式 (使用JavaScript)

    1. 问题引入: 前面我们是使用方法 降低屏幕亮度(不常用) 和 替换theme,两者都是针对Activity的背景进行白天.夜间模式的交换,但是如果我们显示的是Html的内容,这个时候改怎么办? 分 ...

  7. 使用 libffi 实现 AOP

    核心还是利用oc消息的查找派发机制,进行类结构的动态修改,用新函数替换老函数,然后再调用老函数. 前言     众所周知,使用runtime的提供的接口,我们可以设定原方法的IMP,或交换原方法和目标 ...

  8. NPOI保存到服务器和导出到客户端

    保存到服务器 <a class="easyui-linkbutton" href="javascript:void(0);" onclick=" ...

  9. Hadoop学习之路(十三)MapReduce的初识

    MapReduce是什么 首先让我们来重温一下 hadoop 的四大组件: HDFS:分布式存储系统 MapReduce:分布式计算系统 YARN:hadoop 的资源调度系统 Common:以上三大 ...

  10. 【VSC】我安装了哪些扩展插件

    Nodejs gitk  ——  版本实时比对 Debugger for Chrome ——  让 vscode 映射 chrome 的 debug功能,静态页面都可以用 vscode 来打断点调试. ...