MySQL数据库篇之pymysql模块的使用
主要内容:
一、pymysql模块的使用
二、pymysq模块增删改查
1️⃣ pymsql模块的使用
1、前言:之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库,
那如何在python程序中操作数据库呢?这就用到了pymysql模块,该模块本质就是一个套接字
客户端软件,使用前需要事先安装。
pip3 install pymysql
2、实例:
#!/user/bin/env python3
# -*- coding:utf-8-*-
# write by congcong import pymysql user = input('user>>:').strip()
pwd = input('password>>:').strip()
# 建立链接
conn=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='',
db='db6',
charset='utf8'
)
# 拿到游标
cursor = conn.cursor() # 执行完毕后返回的结果集默认以元祖显示
# 执行sql语句
sql = 'select * from userinfo where name="%s" and pwd="%s"' %(user,pwd)
print(sql)
rows = cursor.execute(sql) # 受影响的行数,execute表示执行 cursor.close() # 关闭游标
conn.close() # 关闭链接 if rows:
print('登陆成功!')
else:
print('失败...')
注意:
这种方式存在很大的隐患,即sql注入问题,可绕过密码登录,如:cc1" -- hello 3、execute()之sql注入
注意:符号--会注释掉它之后的sql,正确的语法:--后至少有一个任意字符
user>>:cc1" -- hello # “ 对应左边的左引号,”--“ 在sql语句中表示注释后面的语句,注意中间有空格
password>>:
select * from userinfo where name="cc1" -- hello" and pwd=""
登陆成功!
# 甚至可同时绕过用户名和密码,如:xxxx" or 1=1 -- hello
'''
user>>:xxx" or 1=1 -- hello # or表示或,用户名满足任意一条件即可
password>>:
select * from userinfo where name="xxx" or 1=1 -- hello" and pwd=""
登陆成功!
'''
上述程序的改进版如下:
#!/user/bin/env python3
# -*- coding:utf-8-*-
# write by congcong import pymysql
# 改进版如下:
user = input('用户名>>:').strip()
pwd = input('密码>>:').strip()
# 建立链接
conn = pymysql.connect(
host='localhost',
port= 3306,
user = 'root',
password = '',
db='db6',
charset = 'utf8'
)
# 拿到游标
cursor = conn.cursor()
# 执行
sql = 'select * from userinfo where name=%s and pwd=%s'
# print(sql)
rows = cursor.execute(sql,(user,pwd)) cursor.close()
conn.close()
if rows:
print('welcome login!')
else:
print('failed...')
2️⃣ pymysq模块增删改查
1、插入数据
#!/user/bin/env python3
# -*- coding:utf-8-*-
# write by congcong
import pymysql #1、建链接
conn = pymysql.connect(
host = '127.0.0.1', # 本地主机
port = 3306, # mysql默认端口
user = 'root', # MySQL用户
password = '', # 用户密码
db = 'db6', # 数据库
charset = 'utf8' # 字符编码
) # 2、取游标
cursor = conn.cursor() # 3、执行sql语句
'''
# 插入数据前
mysql> select * from userinfo;
+----+------+-----+
| id | name | pwd |
+----+------+-----+
| 1 | cc1 | 123 |
| 2 | hyt | 456 |
+----+------+-----+
2 rows in set (0.00 sec) # 插入(增)
sql = 'insert into userinfo(name,pwd) values(%s,%s)'
# rows = cursor.execute(sql,('cc2','666')) # 插入单行语句
rows = cursor.executemany(sql,[('hy1','514'),('hyt2','0514'),('cc2','666888')]) # 插入多条数据
print(rows) # 插入数据后
mysql> select * from userinfo;
+----+------+--------+
| id | name | pwd |
+----+------+--------+
| 1 | cc1 | 123 |
| 2 | hyt | 456 |
| 3 | cc2 | 666 |
| 4 | hy1 | 514 |
| 5 | hyt2 | 514 |
| 6 | cc2 | 666888 |
+----+------+--------+
6 rows in set (0.00 sec)
'''
sql = 'insert into userinfo(name,pwd) values (%s,%s)'
rows = cursor.executemany(sql,[('sc1','111'),('sc2','222')])
print(rows) # 2
# 查询表中最后一条数据的id,必须在插入后查询
print(cursor.lastrowid) # 7,返回的数据是现在插入的数据起始id
'''
mysql> select * from userinfo;
+----+------+-----+
| id | name | pwd |
+----+------+-----+
| 1 | 0 | 123 |
| 2 | hyt | 456 |
| 3 | cc2 | 666 |
| 7 | sc1 | 111 |
| 8 | sc2 | 222 |
+----+------+-----+
5 rows in set (0.00 sec)
'''
conn.commit() # 更新数据表,更新后才能在查询插入后的结果 cursor.close() # 关闭游标
conn.close() # 关闭链接
2、删除数据
import pymysql #1、建链接
conn = pymysql.connect(
host = '127.0.0.1', # 本地主机
port = 3306, # mysql默认端口
user = 'root', # MySQL用户
password = '', # 用户密码
db = 'db6', # 数据库
charset = 'utf8' # 字符编码
) # 2、取游标
cursor = conn.cursor() # 3、执行sql语句
# 删除
sql = 'delete from userinfo where id=%s'
# rows=cursor.execute(sql,6) # 删除一条
rows=cursor.executemany(sql,[4,5,6]) # 删除多条 mysql> select * from userinfo;
+----+------+-----+
| id | name | pwd |
+----+------+-----+
| 1 | cc1 | 123 |
| 2 | hyt | 456 |
| 3 | cc2 | 666 |
+----+------+-----+
3 rows in set (0.00 sec)
conn.commit() # 更新数据表 cursor.close() # 关闭游标
conn.close() # 关闭链接
3、修改数据
import pymysql #1、建链接
conn = pymysql.connect(
host = '127.0.0.1', # 本地主机
port = 3306, # mysql默认端口
user = 'root', # MySQL用户
password = '', # 用户密码
db = 'db6', # 数据库
charset = 'utf8' # 字符编码
) # 2、取游标
cursor = conn.cursor() # 3、执行sql语句
# 修改
sql = 'update userinfo set name="cc" where id=%s '
cursor.execute(sql,1) mysql> select * from userinfo;
+----+------+-----+
| id | name | pwd |
+----+------+-----+
| 1 | cc | 123 |
| 2 | hyt | 456 |
| 3 | cc2 | 666 |
+----+------+-----+
3 rows in set (0.00 sec)
conn.commit() # 更新数据表 cursor.close() # 关闭游标
conn.close() # 关闭链接
4、查询数据
import pymysql #1、建链接
conn = pymysql.connect(
host = '127.0.0.1', # 本地主机
port = 3306, # mysql默认端口
user = 'root', # MySQL用户
password = '', # 用户密码
db = 'db6', # 数据库
charset = 'utf8' # 字符编码
) # 2、取游标
cursor = conn.cursor() # 3、执行sql语句
查询数据 cursor = conn.cursor(pymysql.cursors.DictCursor) # 取游标,数据以字典形式取出
sql = 'select * from userinfo'
rows = cursor.execute(sql) # 执行sql语句,返回sql影响成功的行数rows,将结果放入一个集合,等待被查询
# rows = cursor.execute('select * from userinfo;')
# fetchone 一次取一条数据
# print(cursor.fetchone()) # {'id': 1, 'name': '0', 'pwd': 123}
# print(cursor.fetchone())
# print(cursor.fetchone())
# print(cursor.fetchone()) # 不会报错,取完后返回None # fetchmany 一次取多条数据,超过数据条数大小时不报错
#print(cursor.fetchmany(2)) # [{'id': 1, 'name': '0', 'pwd': 123}, {'id': 2, 'name': 'hyt', 'pwd': 456}] # fetchall 一次取出全部数据
# print(cursor.fetchall()) # cursor.scroll(1,mode='absolute') # 相对绝对位置移动,从头向后移动一条数据
# print(cursor.fetchone()) # {'id': 2, 'name': 'hyt', 'pwd': 456}
print(cursor.fetchone())
cursor.scroll(1,mode='relative') # 相对当前位置移动,从上一条查询数据向后移动一条数据
print(cursor.fetchone())
'''
conn.commit() # 更新数据表 cursor.close() # 关闭游标
conn.close() # 关闭链接
MySQL数据库篇之pymysql模块的使用的更多相关文章
- [MySQL数据库之Navicat.pymysql模块、视图、触发器、存储过程、函数、流程控制]
[MySQL数据库之Navicat.pymysql模块.视图.触发器.存储过程.函数.流程控制] Navicat Navicat是一套快速.可靠并价格相当便宜的数据库管理工具,专为简化数据库的管理及降 ...
- 第二百八十九节,MySQL数据库-ORM之sqlalchemy模块操作数据库
MySQL数据库-ORM之sqlalchemy模块操作数据库 sqlalchemy第三方模块 sqlalchemysqlalchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API ...
- SQL学习笔记六之MySQL数据备份和pymysql模块
mysql六:数据备份.pymysql模块 阅读目录 一 IDE工具介绍 二 MySQL数据备份 三 pymysql模块 一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测 ...
- MySQL 数据备份,Pymysql模块(Day47)
阅读目录 一.IDE工具介绍 二.MySQL数据备份 三.Pymysql模块 一.IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https:/ ...
- python操作MySQL数据库的三个模块
python使用MySQL主要有两个模块,pymysql(MySQLdb)和SQLAchemy. pymysql(MySQLdb)为原生模块,直接执行sql语句,其中pymysql模块支持python ...
- mysql 数据备份。pymysql模块
阅读目录 一 IDE工具介绍 二 MySQL数据备份 三 pymysql模块 一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https:/ ...
- 基于Python的接口自动化实战-基础篇之pymysql模块操作数据库
引言 在进行功能或者接口测试时常常需要通过连接数据库,操作和查看相关的数据表数据,用于构建测试数据.核对功能.验证数据一致性,接口的数据库操作是否正确等.因此,在进行接口自动化测试时,我们一样绕不开接 ...
- python数据库操作之pymysql模块和sqlalchemy模块(项目必备)
pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 1.下载安装 pip3 install pymysql 2.操作数据库 (1).执行sql #! ...
- mysql用户管理和pymysql模块
一:mysql用户管理 mysql是一个tcp的服务器,用于操作服务器上的文件数据,接收用户端发送的指令,而接收指令时就 需要考虑安全问题. 在mysql自带的数据库中有4个表是用于用户管理的,分别是 ...
随机推荐
- Vue生命周期函数详解
vue实例的生命周期 1 什么是生命周期(每个实例的一辈子) 概念:每一个Vue实例创建.运行.销毁的过程,就是生命周期:在实例的生命周期中,总是伴随着各种事件,这些事件就是生命周期函数: 生命周期: ...
- 【DUBBO】zookeeper在dubbo中作为注册中心的原理结构
[一]原理图 [二]原理图解释 流程:1.服务提供者启动时向/dubbo/com.foo.BarService/providers目录下写入URL2.服务消费者启动时订阅/dubbo/com.foo. ...
- C#封装的一个JSON操作类
using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...
- fatal: The remote end hung up unexpectedly解决办法
$ git config --global http.postBuffer 2428000 git config http.postBuffer 524288000 配置完成后 git pull一下, ...
- Git 的分支和标签规则
Git 的分支和标签规则 分支使用 x.x 命名,不加 V. 标签使用 v1.x.x-xxx 方式命名.(v 为小写) 分支和标签名不可重复.
- CentOS6.5安装中文支持
本人在安装CentOS6.5时选择是英文版,安装后打开文档,发现好些文档成了乱码了. 这个问题的原因是没有中文支持. 解决方法: 1.安装中文支持包 # yum groupinstall " ...
- 16.Python使用lxml爬虫
1.lxml是解析库,使用时需要导入该包,直接在命令行输入:pip3 install lxml,基本上会报错.正确应该去对应的网址:https://pypi.org/project/lxml/#fil ...
- (转)Inno Setup入门(十)——操作注册表
本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250871 有些程序需要随系统启动,或者需要建立某些文件关联等问题 ...
- windows下搭建nginx-rtmp服务器
windows下搭建nginx-rtmp服务器 windows下搭建nginx-rtmp服务器 准备工作 安装MinGW 安装Mercurial 安装strawberryperl 安装nasm 下载n ...
- python--logging库学习_自我总结---有空完善
思路: 1.把前面的都封装,然后在测试用例里面调用,每一步测试步骤下面都加一个 logging.info('这个是测试步骤')(可以 亲测) 2.尝试添加到unittest框架里面,看能不能一起使用 ...