主要内容:

  一、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. python(七):元类与抽象基类

    一.实例创建 在创建实例时,调用__new__方法和__init__方法,这两个方法在没有定义时,是自动调用了object来实现的.python3默认创建的类是继承了object. class A(o ...

  2. 《DSP using MATLAB》示例Example 8.4

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  3. [datatables杂记] sAjaxSource 数据源 Search 后 fnInitComplete 不执行。

    var oTable = $('#div_list').dataTable({ "oLanguage": {//语言国际化 "sUrl": "/Adm ...

  4. ballerina 学习十四 values && types

    ballerina 包含的数据类型有string int map array record boolean ojbect function table tuple any 简单说明 数据类型和其他语言 ...

  5. c++中子类转父类,父类转子类

    #include <iostream> using namespace std; class Father { public: virtual void show() { cout< ...

  6. RabbitMQ消息的消费与持久化

    作为消费者的客户端要消费Rabbitmq的消息,首先要建立与它某个队列的连接,具体连接时可指定队列的BindingKey和关系的exchange标识,Rabbitmq判断若已有队列通过BindingK ...

  7. (转)Linux查看CPU,硬盘,内存的大小

         分类: linux(21)  在Linux的桌面版本中,查看这些东西的确很方便,有图形化的工具可以使用.但是在Linux服务器版上,或者远程ssh连接的时候,就没有图形化的界面可以操作了.此 ...

  8. (转)如何获得当前ListVIew包括下拉的所有数据?

    ListView listView = activity.getListView();获取的仅仅是当前屏幕显示的list,但是具有下拉信息,不在当前屏幕,但是下拉显示的数据无法或得到.谁知道如何获得当 ...

  9. SVN checkout 出的项目在PHPstorm中打开没有subversion(SVN)选项按钮怎么办?

    对于svn add 命令的批量操作,为了操作简便还是习惯在IDE中完成,有时候新checkout出的项目,在PHPstorm中右键菜单中没有 Subversion 按钮,操作如下: 点击VCS按钮,然 ...

  10. python3api-ms-win-crt-runtime-l1-1-0.dll丢失解决方法

    先记录一个之前遇到的问题: 在安装了pycharm后,发现 通过上网发现,其实就是没有安装pip和setuptools,其实 Python3以后都是默认安装pip的,所以最后的解决办法是将我目前的Py ...