Title

pymysql模块的介绍

pymysql 模块

1.什么是PYMySQL 模块

pymysql模块是用来将pycharm和mysql数据库连接的第三方模块

通过pip install pymysql 的方式可以下载安装此模块

import pymysql
conn = pymysql.connect(    
    host = '127.0.0.1',  # 访问数据库的ip地址    
    port = 3306,   # 访问数据库的端口,默认3306    
    user = 'root',  # 用户名    
    password = '960617',  # 密码    
    charset = 'utf8', # 编码格式    
    database = 'db1'  # 访问的库
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)    # 产生一个游标对象
# curson=pymysql.cursors.DictCursor 将查出来的结果制作成字典的形式返回
# cursor = conn.cursor() # 若不使用查出来的结果是元组套元组的形式
sql = 'select * from t1' # 正常报黄,不要担心

res = cursor.execute(sql)  # 执行sql语句
print(res)  # 返回的是当前sql语句影响的行数
#
ret1 = cursor.fetchone() # 获取的是查询结果中的一条数据
print(ret1)
# ret2 = cursor.fetchone() # 如果有多条fetchone语句,会逐一获取数据,而不会一直获取第一条
# print(ret2)
# ret3 = cursor.fetchone()
# print(ret3)
#
# ret4 = cursor.fetchall()
# print(ret4)

# ret5 = cursor.fetchall()  # fetchall表示查询全部数据
# print(ret5)
#
# ret6 = cursor.fetchmany(2) # 表示获取指定几条的数据
# print(ret6)
# 
# # 相对移动
# cursor.scroll(1, 'relative') # 基于指针所在位置,往后偏远指定数字的数据
# print(cursor.fetchall())

# 绝对移动
cursor.scroll(1, 'absolute') # 基于起始位置往后偏移指定数字的数据
print(cursor.fetchall())

# 这两种移动都不能超过真实数据长度,否则会报错

cursor.close()

# 6.关闭客户端连接
conn.close()

2.sql语句注入问题

import pymysql

conn = pymysql.connect(
    host = '127.0.0.1',  # 访问数据库的ip地址
    port = 3306,   # 访问数据库的端口,默认3306
    user = 'root',  # 用户名
    password = '960617',  # 密码
    charset = 'utf8', # 编码格式
    database = 'db1'  # 访问的库
)
cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
username = input('请输入用户名: ').strip()
age = input('请输入年龄: ').strip()

sql = "select * from t1 where name = '%s' and age = '%s'"%(username, age)

print(sql)  # 将语句打印出来
cursor.execute(sql)
res = cursor.fetchall()
if res:
    print(res)
else:
    print('username or password error!')
    
#正常情况下

# 非正常情况下(第一种)
请输入用户名:tank'-- sjiajs  
    # 通过(' -- sjiajs) 的方式伪装成被注释的字句从而绕过校验
请输入年龄: 
select * from t1 where name = 'tank'-- sjiajs' and age = ''
[{'id': 2, 'name': 'tank', 'age': 18}]

Process finished with exit code 0


# 非正常情况下(第二种)
请输入用户名: xxx' or 1 = 1 -- nsjanksmals
    # 通过(' or 1 = 1 -- nsjanksmals)让where改为判断1=1 是否为真从而绕过判断
请输入年龄: 
select * from t1 where name = 'xxx' or 1 = 1 -- nsjanksmals' and age = ''
[{'id': 1, 'name': 'sean', 'age': 17}, {'id': 2, 'name': 'tank', 'age': 18}, {'id': 3, 'name': 'jason', 'age': 20}, {'id': 4, 'name': 'owen', 'age': 0}]


总结:
    """
sql注入问题
    利用特殊符号和注释语法 巧妙的绕过真正的sql校验

关键性的数据 不要自己手动去拼接 而是交由execute帮你去做拼接
""""
    
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
username = input('请输入用户名: ').strip()
age = input('请输入年龄: ').strip()

# sql = "select * from t1 where name = '%s' and age = '%s'"%(username, age)
sql = "select * from t1 where name = %s and age = %s"  # 重要数据不要自己手动传输

print(sql)  # 将语句打印出来
cursor.execute(sql, (username, age)) # execute自带有传输数据的功能,为了防止特殊字符而设置的
res = cursor.fetchall()
if res:
    print(res)
else:
    print('username or password error!')
 

# 验证
请输入用户名: tank' -- jsisjsl
请输入年龄: 
select * from t1 where name = %s and age = %s
username or password error!

Process finished with exit code 0

3.pymysql的增删改查

import pymysql
conn = pymysql.connect(
    user = 'root',
    password = '960617',
    port = 3306,
    host = '127.0.0.1',
    charset = 'utf8',
    database = 'db1',
    autocommit = True  # 自动确认执行操作
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

# 增
sql = "insert into t1(name,age) values ('jiang', 16)"

# conn.commit()  # 若没有设置自动确认操作,则需要此手动操作确认执行
# 改
sql = "update t1 set name='egondsb' where id = 1"

# 删除
# sql = "delete from t1 where id= 1"


res = cursor.execute(sql)
print(res)


总结:
"""
针对增 删 改操作 执行重要程度偏高 
你如果真想操作  必须有一步确认操作(commit)
"""





posted @ 2019-12-16 20:58  Mr江  阅读(537)  评论(0编辑  收藏  举报