主要内容:

  一、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. PHP5.3、PHP5.4、PHP5.5、PHP5.6的新特性

    1. PHP5.3中的新特性 1.1 支持命名空间(namespace) 毫无疑问,命名空间是PHP5.3所带来的最重要的新特性. 在PHP5.3中,可以用命名空间防止代码的冲突,命名空间的分隔符为 ...

  2. BW处理链(Process Chain)

    处理链是能自动完成数据的处理和加载等操作的自动化工具.   1.创建处理链 输入T-code:RSPC打开操作界面,或者处理链已经在T-code:RSA1=>Modeling界面下,也可以直接单 ...

  3. vue 中import和export如何一起使用(一)

    前段时间碰到一个问题,vue js中要使用import来加载第三方的js,但是后面使用exports.XXX的话会报exports is not defined.那要怎么解决呢? 首先,我们要了解ES ...

  4. WPF自适应可关闭的TabControl 类似浏览器的标签页(转)

    效果如图: 虽然说是自适应可关闭的TabControl,但TabControl并不需要改动,不如叫自适应可关闭的TabItem. 大体思路:建一个用户控件,继承自TabItem,里面放个按钮,点击的时 ...

  5. Nomad 了解

    Introduction to Nomad Welcome to the intro guide to Nomad! This guide is the best place to start wit ...

  6. Springboot的优点和实现

    一 优点 1.创建独立的Spring应用程序 2.嵌入式的Tomcat,不需要部署war包 3.简化Maven配置 4.自动配置Spring 5.提供生产就绪型功能,如指标,健康检查,和外部配置 6. ...

  7. CRC 自动判断大端 小端

    /* aos_crc64.c -- compute CRC-64 * Copyright (C) 2013 Mark Adler * Version 1.4 16 Dec 2013 Mark Adle ...

  8. Oracle事务的ACID特性

    Oracle事务的ACID特性 1.原子性(Atomicity) 事务的原子性是指事务中包含的所有操作要么都做,要么都不做,保证数据库是一致的. 例如:A帐户向B帐户划账1000,则先将A减少1000 ...

  9. Form 总结

    禁止input自动完成下拉 //ie: autocomplete="off" //ff: disableautocomplete <input size="40&q ...

  10. java代码---实现随机产生1000个随机数,并10个一行的输出

    总结:不会用,就是不熟 package com.s.x; //输入10个随机数,并显示最大值,最小值 import java.util.*; public class Value { public s ...