pymysql模块


1.pymysql介绍

pymysql是为python操作MySQL而提供的模块,使用非常方便
注意:MySQL默认的端口为3306,不建议更改配置

pymysql的基本使用
模拟登陆验证
mysql服务端准备

create database db3 charset = "utf8";
use db3;
create table userinfo(
    id int primary key auto_increment,
    name char(10) not null,
    password char(16) not null
);

insert into userinfo values
    (1,"xiaoqing","abc123"),
    (2,"qingqing","abc123"),
    (3,"honghong","abc123"),
    (4,"nana","abc123"),
    (5,"feifie","abc123");
    

客户端代码

import pymysql

user = input("user>:").strip()
pwd = input("pwd>:").strip() 

#建立链接
conn = pymysql.connect(
    host = '127.0.0.1', #本地测试
    port = 3306, #默认端口
    user = 'root',
    password = 'abc123',
    db = 'db3',   
    charset = 'utf8'
)

#拿到游标,游标是提交MySQL命令的接口 
cursor = conn.cursor() # 可以写参数pymysql.cursors.DictCursor,是基于字典的游标

#执行sql语句(注意,这里有个坑!sql注入的问题)
sql = "select * from userInfo where name = '%s' and password = '%s';"%(user,pwd) 

rows = cursor.execute(sql)#拿到执行的结果,受影响的条数,但是不是查询的结果
#0代表没有查询到!说明没有相关信息

#使用完了一定要关闭游标和链接
cursor.close() #关闭游标
conn.close() #关闭链接

if rows:
    print("登陆成功!")
else:
    print("登录失败")
    

注意:mysql就是一个套接字软件,就要遵循一大堆协议。客户端建立了连接,3次握手完成,当不再使用数据库时,得关闭链接,完成4次挥手!


2.避免sql注入的问题

演示

import pymysql

name = input("user>:").strip()
pwd = input("pwd>:").strip()

conn = pymysql.connect(
    host = '127.0.0.1',
    port = 3306,
    user = 'root',
    password = 'abc123',
    db = 'db3',
    charset = 'utf8'
)

cursor = conn.cursor()

sql = 'select * from userinfo where name = "%s" and password = "%s";'%(name,pwd)
rows = cursor.execute(sql)
print(sql)

cursor.close()
conn.close() 

if rows:
    print("登录成功!")
else:
    print("登录失败!") 
    

测试演示

#一:
user>:xiaoqing" -- xxxxx
pwd>:
select * from userinfo where name = "xiaoqing" -- xxxxx" and password = "";
登录成功!

#二:
user>:xxx" or 1=1 -- 就是不输入用户名和密码
pwd>:
select * from userinfo where name = "xxx" or 1=1 -- 就是不输入用户名和密码" and password = "";
登录成功!

这里成功的骗过了mysql不输入任何用户信息或者密码就能查询到信息,怎么避免这种sql注入问题呢?还好,pymysql模块为我们提供了解决这类问题的方式,我们就可以不用重复造轮子;在写sql语句的时候我们不自己拼接,写个占位符%s,然后用execute自己拼接,它会自动判断输入是否合法,将有不怀好意的输入统统过滤掉

import pymysql

name = input("user>:").strip()
pwd = input("pwd>:").strip()

#建立连接
conn = pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user="root",
    password="abc123",
    db="db3",
    charset="utf8"
)

cursor = conn.cursor()

sql = 'select * from userInfo where name = %s and password = %s'
rows = cursor.execute(sql,(name,pwd))
#让execute进行拼接sql语句,过滤到不合法的输入
#拿到执行的结果,受影响的条数,但是不是查询的结果
#0代表没有查询到!说明没有相关信息

cursor.close() #关闭游标
conn.close() #关闭链接

#进行判断
if rows:
    print("登陆成功!")
else:
    print("登录失败")

测试演示

#一:
user>:xiaoqing" --不要密码
pwd>:
登录失败

#二:
user>:dfedfef" or 1=1 -- no密码哈哈
pwd>:
登录失败


3.增删改查操作

增删改都涉及到数据库的更新,可以放在一起描述

以增为例

import pymysql

conn = pymysql.connect( #连接
    host = "127.0.0.1",
    port = 3306,
    user = 'root',
    password = 'abc123',
    db='db3',
    charset = 'utf8'
)

cursor = conn.cursor() #游标接口

sql = 'insert into userinfo(name,password) values(%s,%s);'

rows_1 = cursor.execute(sql,("mingming",'abc123')) #插入一条记录
rows_2 = cursor.executemany(sql,[("xiaohong","abc123"),("xiaoxiang","abc123")]) #插入多条记录
print(cursor.lastrowid) #写在插入语句之后,获取插入的最后一条数据的自增ID,显示字段走到哪里了,

conn.commit() #增删改一定要写这个功能,这样才能把相关操作命令提交到数据库,数据库才执行之前写的sql语句,这个语句的作用是为了保证数据的安全

cursor.close()
conn.close()

查询操作

import pymysql

#建立链接
conn = pymysql.connect(
    host = "127.0.0.1",
    port = 3306,
    user = 'root',
    password = "abc123",
    db = 'db3',
    charset = "utf8"
)

cursor = conn.cursor(pymysql.cursors.DictCursor)
#基于字典的游标,方便查看查询结果
rows = cursor.execute('select * from userInfo;')

#打印结果,光标的描述是从0开始的!!
print(cursor.fetchone()) #相当于文件的光标移动,取一次,移动一次
cursor.scroll(2,mode='absolute') # 相对绝对位置移动2位,0对应第一条
cursor.scroll(2,mode='relative') # 相对当前位置移动2位
print(cursor.fetchone())

print(cursor.fetchone()) #一次去一条查询结果
print(cursor.fetchmany(4)) #从光标的位置取指定个数
print(cursor.fetchall()) #从光标的位置全取出

cursor.close() #关闭游标
conn.close()  #关闭链接

posted @ 2018-04-24 19:16  哈哈大圣  阅读(86)  评论(0编辑  收藏  举报