pymysql模块的使用

本节重点:

  •   pymysql的下载和使用
  •   execute()之sql注入
  •   增、删、改:conn.commit()
  •   查:fetchone、fetchmany、fetchall

一、pymysql的下载

  之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库,那如何在python程序中操作数据库呢?这就用到了pymysql模块,该模块本质就是一个套接字客户端软件,使用前需要事先安装。

pip3 install pymysql

二、pymysql的使用

实现:使用Python实现用户登录,如果用户存在则登录成功(假设该用户已在数据库中)

import pymysql
user = input('请输入用户名:').strip()
pwd = input('请输入密码:').strip() # 1.连接
#创建一个连接对象<pymysql.connections.Connection object at 0x005F2910>
conn = pymysql.connect(host='192.168.76.136', port=3306, user='root', password='root', db='db1', charset='utf8') # 2.创建游标
#创建一个游标对象<pymysql.cursors.Cursor object at 0x005E0290>
cursor = conn.cursor() #注意%s需要加引号
sql = 'select * from user where username="%s" and password="%s"'%(user, pwd) print(sql) # 3.执行sql语句
result = cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目,不是查询内容
print(result) # 4.关闭连接,游标和连接都要关闭
cursor.close()
conn.close() #打印结果时可以根据判断输出不同结果
if result:
print('登陆成功')
else:
print('登录失败') 或
print('登录成功') if res else print('登录失败')

三、execute()之sql注入

最后那一个空格,在一条sql语句中如果遇到select * from userinfo where username='user1' -- asadasdas' and pwd='' 则--之后的条件被注释掉了(注意--后面还有一个空格)

#1、sql注入之:用户存在,绕过密码
user1' -- 任意字符

#2、sql注入之:用户不存在,绕过用户与密码
xxx' or 1=1 -- 任意字符

解决方法: 

# 原来是我们对sql进行字符串拼接
# sql="select * from userinfo where name='%s' and password='%s'" %(username,pwd)
# print(sql)
# result=cursor.execute(sql) #改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了)
sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上
result=cursor.execute(sql,[user,pwd]) #pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。 sql = 'select * from user where username=%s and password=%s'
res = cursor.execute(sql, (user, pwd))

四、增、删、改:conn.commit()

commit()方法:在数据库里增、删、改的时候,必须要进行提交,否则插入的数据不生效。

import pymysql

user = input('请输入用户名:').strip()
pwd = input('请输入密码:').strip() conn = pymysql.connect(host='192.168.76.136', port=3306, user='root', password='root', db='db1', charset='utf8')
cursor = conn.cursor()

#定义insert语句

sql_insert = 'insert into user (username, password) values (%s, %s)'

res = cursor.execute(sql_insert, (user, pwd))
#一定要commit
conn.commit() cursor.close()
conn.close() print('登录成功') if res else print('登录失败') mysql> select * from user;
+----+----------+----------+----------------+
| id | username | password | emall |
+----+----------+----------+----------------+
| 1 | user1 | 123 | user1@test.com |
| 2 | user2 | 123 | user2@test.com |
| 3 | user3 | 123 | NULL |
+----+----------+----------+----------------+

#添加多条记录,参数是一个列表中的多个集合

res = cursor.executemany(sql_insert, [('aaa',123),('bbb',123)])
conn.commit()
cursor.close()
conn.close() mysql> select * from user;
+----+----------+----------+----------------+
| id | username | password | emall |
+----+----------+----------+----------------+
| 1 | user1 | 123 | user1@test.com |
| 2 | user2 | 123 | user2@test.com |
| 3 | user3 | 123 | NULL |
| 4 | aaa | 123 | NULL |
| 5 | bbb | 123 | NULL |
+----+----------+----------+----------------+

#定义修改update语句

sql_update = 'update user set username = %s where id=2'
res = cursor.execute(sql_update, user)
conn.commit()
cursor.close()
conn.close() mysql> select * from user;
+----+----------+----------+----------------+
| id | username | password | emall |
+----+----------+----------+----------------+
| 1 | user1 | 123 | user1@test.com |
| 2 | test1 | 123 | user2@test.com |
| 3 | user3 | 123 | NULL |
| 4 | aaa | 123 | NULL |
| 5 | bbb | 123 | NULL |
+----+----------+----------+----------------+

#定义删除delete语句

sql_delete = 'delete from user where id=4'
res = cursor.execute(sql_delete)
conn.commit()
cursor.close()
conn.close() mysql> select * from user;
+----+----------+----------+----------------+
| id | username | password | emall |
+----+----------+----------+----------------+
| 1 | user1 | 123 | user1@test.com |
| 2 | test1 | 123 | user2@test.com |
| 3 | user3 | 123 | NULL |
| 5 | bbb | 123 | NULL |
+----+----------+----------+----------------+

五、查:fetchone、fetchmany、fetchall

fetchone():获取下一行数据,第一次为首行,可多次执行。
fetchall():获取所有行数据源
fetchmany(4):获取4行数据,n可指定

查看表内容:

1、fetchone()

import pymysql
conn = pymysql.connect(host='192.168.76.136', port=3306, user='root', password='root', db='db1', charset='utf8')
cursor = conn.cursor() #定义查询语句
sql_select = 'select * from user ' res = cursor.execute(sql_select) row = cursor.fetchone()
print(row)
row = cursor.fetchone()
print(row) cursor.close()
conn.close() 结果:每执行一次查询一行,可多次执行逐行查看
(1, 'user1', 123, 'user1@test.com')
(2, 'test1', 123, 'user2@test.com')

2、fetchmany(n)

row = cursor.fetchmany(3)
print(row)
结果:显示3条记录
((1, 'user1', 123, 'user1@test.com'), (2, 'test1', 123, 'user2@test.com'), (3, 'user3', 123, None))

3、cursor.fetchall()

row = cursor.fetchall()
print(row)
结果:显示所有记录
((1, 'user1', 123, 'user1@test.com'), (2, 'test1', 123, 'user2@test.com'), (3, 'user3', 123, None), (5, 'bbb', 123, None))

4、DictCursor

默认情况下,我们获取到的返回值是元组,在获取数据的时候并不方便,可以使用以下方式来返回字典,每一行的数据都会生成一个字典:
#在实例化conn的时候,将属性cursor设置为pymysql.cursors.DictCursor
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 结果:
[{'id': 1, 'username': 'user1', 'password': 123, 'emall': 'user1@test.com'}, {'id': 2, 'username': 'test1', 'password': 123, 'emall': 'user2@test.com'}, {'id': 3, 'username': 'user3', 'password': 123, 'emall': None}, {'id': 5, 'username': 'bbb', 'password': 123, 'emall': None}]

5、指针位置移动

在fetchone示例中,在获取行数据的时候,可以理解开始的时候,有一个行指针指着第一行的上方,获取一行,它就向下移动一行,所以当行指针到最后一行的时候,就不能再获取到行的内容,所以我们可以使用如下方法来移动行指针:

cursor.scroll(1,mode='relative')  # 相对当前位置移动
cursor.scroll(2,mode='absolute') # 相对绝对位置移动
第一个值为移动的行数,整数为向下移动,负数为向上移动,mode指定了是相对当前位置移动,还是相对于首行移动 sql = 'select * from user'
cursor.execute(sql) # 查询第一行的数据
row = cursor.fetchone()
print(row) # 查询第二行数据
row = cursor.fetchone()
print(row) cursor.scroll(-1,mode='relative') #设置之后,光标相对于当前位置(第3行)往前移动了一行,所以打印的结果为第二行的数据
row = cursor.fetchone()
print(row) cursor.scroll(0,mode='absolute') #设置之后,光标相对于首行没有任何变化,所以打印的结果为第一行数据
row = cursor.fetchone()
print(row)

day44-pymysql模块的使用的更多相关文章

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

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

  2. python实战第一天-pymysql模块并练习

    操作系统 Ubuntu 15.10 IDE & editor JetBrains PyCharm 5.0.2 ipython3 Python版本 python-3.4.3 安装pymysql模 ...

  3. pymysql 模块介绍

    pymysql模块是python与mysql进行交互的一个模块. pymysql模块的安装: pymysql模块的用法: import pymysql user=input('user>> ...

  4. Mysql(六):数据备份、pymysql模块

    一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https://pan.baidu.com/s/1bpo5mqj 掌握: #1. 测试+链接 ...

  5. python如何使用pymysql模块

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

  6. MySQL之pymysql模块

    MySQL之pymysql模块   import pymysql #s链接数据库 conn = pymysql.connect( host = '127.0.0.1', #被连接数据库的ip地址 po ...

  7. PyMySQL模块的使用

    PyMySQL介绍 PyMySQL是在Python3.x版本中用于连接MySQL服务器的一个库,Python2系列中则使用mysqldb.Django中也可以使用PyMySQL连接MySQL数据库. ...

  8. MySQL学习12 - pymysql模块的使用

    一.pymysql的下载和使用 1.pymysql模块的下载 2.pymysql的使用 二.execute()之sql注入 三.增.删.改:conn.commit() 四.查:fetchone.fet ...

  9. 数据库入门-pymysql模块的使用

    一.pymysql模块安装 由于本人的Python版本为python3.7,所以用pymysql来连接数据库(mysqldb不支持python3.x) 方法一: #在cmd输入 pip3 instal ...

  10. Python连接MySQL数据库之pymysql模块使用

    安装PyMySQL pip install pymysql PyMySQL介绍 PyMySQL是在python3.x版本中用于连接MySQL服务器的一个库,2中则使用mysqldb. Django中也 ...

随机推荐

  1. Hadoop概念学习系列之关于hadoop-2.2.0和hadoop2.6.0的winutils.exe、hadoop.dll版本混用(易出错)(四十三)

    问题详情是 2016-12-10 23:24:13,317 INFO [org.apache.hadoop.metrics.jvm.JvmMetrics] - Initializing JVM Met ...

  2. C#创建自定义Object对象

    , B=,J=}; 记录一下,老写成  var obj = new object() { O=0, B=0,J=0};

  3. 使用Google cardboard 2的一些软件

    最近入手cardboard2,FQ尝试了一些软件,特别分享,给大家提供一些方便. 链接:http://pan.baidu.com/s/1slehilZ 密码:b49h

  4. Kong网关介绍与安装小记

    本文主要为kong安装小记,系统环境为centos 6.7                                本文转载请注明出处 —— xiaoEight 介绍 Kong 是在客户端和(微 ...

  5. 纯MATLAB版本 SIFT代码

    先贴几个链接: http://blog.csdn.net/abcjennifer/article/details/7639681  Rachel-Zhang的 http://blog.csdn.net ...

  6. go语言学习--channel的关闭

    在使用Go channel的时候,一个适用的原则是不要从接收端关闭channel,也不要在多个并发发送端中关闭channel.换句话说,如果sender(发送者)只是唯一的sender或者是chann ...

  7. ssh 免密码登录linux

    就两步,take it easy! step1. 在A-PC生成公钥和密钥对 ssh-keygen -t rsa step2. 将A-PC公钥上传至B-PC ssh-copy-id abby@.xxx ...

  8. SCCM 2012 R2实战系列之八:OSD(上)--分发全新Windows7系统

    今天将跟大家一起分享SCCM 中最为重要的一个功能---操作系统分发(OSD),在此文章中会讨论到OSD的初始化配置.镜像的导入.任务序列的创建编辑.并解决大家经常遇到的分发windows7系统分区盘 ...

  9. 【 MAKEFILE 编程基础之四】详解MAKEFILE 函数的语法与使用!

    本站文章均为 李华明Himi 原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/gcc-makefile/771.html   ...

  10. 第11章 拾遗3:虚拟局域网(VLAN)

    1. 虚拟局域网(VLAN) (1)VLAN是建立在物理网络基础上的一种逻辑子网,它将把一个LAN划分成多个逻辑的局域网(VLAN),每个VLAN是一个广播域,VLAN内的主机间通信就和在一个LAN内 ...