主要内容:

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

  1. [MySQL数据库之Navicat.pymysql模块、视图、触发器、存储过程、函数、流程控制]

    [MySQL数据库之Navicat.pymysql模块.视图.触发器.存储过程.函数.流程控制] Navicat Navicat是一套快速.可靠并价格相当便宜的数据库管理工具,专为简化数据库的管理及降 ...

  2. 第二百八十九节,MySQL数据库-ORM之sqlalchemy模块操作数据库

    MySQL数据库-ORM之sqlalchemy模块操作数据库 sqlalchemy第三方模块 sqlalchemysqlalchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API ...

  3. SQL学习笔记六之MySQL数据备份和pymysql模块

    mysql六:数据备份.pymysql模块   阅读目录 一 IDE工具介绍 二 MySQL数据备份 三 pymysql模块 一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测 ...

  4. MySQL 数据备份,Pymysql模块(Day47)

    阅读目录 一.IDE工具介绍 二.MySQL数据备份 三.Pymysql模块 一.IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https:/ ...

  5. python操作MySQL数据库的三个模块

    python使用MySQL主要有两个模块,pymysql(MySQLdb)和SQLAchemy. pymysql(MySQLdb)为原生模块,直接执行sql语句,其中pymysql模块支持python ...

  6. mysql 数据备份。pymysql模块

    阅读目录 一 IDE工具介绍 二 MySQL数据备份 三 pymysql模块 一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https:/ ...

  7. 基于Python的接口自动化实战-基础篇之pymysql模块操作数据库

    引言 在进行功能或者接口测试时常常需要通过连接数据库,操作和查看相关的数据表数据,用于构建测试数据.核对功能.验证数据一致性,接口的数据库操作是否正确等.因此,在进行接口自动化测试时,我们一样绕不开接 ...

  8. python数据库操作之pymysql模块和sqlalchemy模块(项目必备)

    pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 1.下载安装 pip3 install pymysql 2.操作数据库 (1).执行sql #! ...

  9. mysql用户管理和pymysql模块

    一:mysql用户管理 mysql是一个tcp的服务器,用于操作服务器上的文件数据,接收用户端发送的指令,而接收指令时就 需要考虑安全问题. 在mysql自带的数据库中有4个表是用于用户管理的,分别是 ...

随机推荐

  1. hadoop入门手册1:hadoop【2.7.1】【多节点】集群配置【必知配置知识1】

    问题导读 1.说说你对集群配置的认识?2.集群配置的配置项你了解多少?3.下面内容让你对集群的配置有了什么新的认识? 目的 目的1:这个文档描述了如何安装配置hadoop集群,从几个节点到上千节点.为 ...

  2. 隐藏控件HiddenField使用

    HiddenField控件顾名思义就是隐藏输入框的服务器控件,它能让你保存那些不需要显示在页面上的且对安全性要求不高的数据. 增加HiddenField,其实是为了让整个状态管理机制的应用程度更加全面 ...

  3. Linux 之 hugepage 大页内存理论

    HugePages是通过使用大页内存来取代传统的4kb内存页面,使得管理虚拟地址数变少,加快了从虚拟地址到物理地址的映射以及通过摒弃内存页面的换入换出以提高内存的整体性能.尤其是对于8GB以上的内存以 ...

  4. 如何将angular-ui-bootstrap的分页组件封装成一个指令

    准备工作: (1)一如既往的我还是使用了requireJS进行js代码的编译 (2)必须引入angualrJS , ui-bootstrap-tpls-1.3.2.js , bootstrap.css ...

  5. spring装载配置文件失败报错:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

    Tomcat容器启动失败,找到 debug日志一看: Context initialization failed org.springframework. beans.factory.xml.XmlB ...

  6. git 修改提交说明 commit message

    修改最近一次的提交说明 1.代码未推送到远程服务器 $ git commit --amend 此指令会打开文本编辑器,第二行就是提交说明,修改完后按 ctrl+x 退出,后面根据提示操作. 2.代码已 ...

  7. Win10系统WMIProviderHost进程占用CPU过高

    “WMI Provider Host“占用了过多CPU资源导致系统卡顿,该如何解决这个问题呢? 解决方法: 可以尝试关闭Windows防火墙服务来解决这个问题. 1.按住win+R,输入service ...

  8. 安装MySQL-python 的问题

    安装MySQL-python 的问题 1.CentOS下载mysql-devel安装 yum install mysql-devel 2.Ubuntu下不叫mysql-devel,而是叫libmysq ...

  9. NGUI中LabelA停靠LabelB的方法

    详情看 http://note.youdao.com/noteshare?id=ec901d56341207052b2d19233b5ddba3 这里仅仅贴出文字,完整内容请看上面链接. 有这样一个需 ...

  10. 如何搭建struts2框架

    一.首先,下载5个Struts2核心jar包: commons-logging-1.1.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar struts2-core- ...