day038 navicat pymysql

今日内容:

1.navicat 

2.pymysql

1.navicat 

需要掌握

#1. 测试+链接数据库
#2. 新建库
#3. 新建表,新增字段+类型+约束
#4. 设计表:外键
#5. 新建查询
#6. 备份库/表

#注意:
批量加注释:ctrl+?键
批量去注释:ctrl+shift+?键

2.pymysql

我们要学的pymysql是用python程序如何操作mysql,本质上就是一个套接字客户端,只不过这个套接字客户端是在python程序中的.

#安装
pip3 install pymysql

连接,执行sql ,关闭游标

import pymysql

user=input('请输入用户名:')
upwd=input('请输入密码:')

conn=pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password='xxx',#密码为字符串类型
    database='xxx',
    charset='utf8')

#创建游标
cur=conn.cursor()

#写指令
sql=input('请输入你的指令:')
#执行sql指令
res=cur.execute(sql)
#注:这里拿到的res是得到的指令的条数

#如果想要拿数据,用fetchall()
all_data=cur.fetchall() #此时返回的是所有的数据,但凡是数据,和读文件差不多,
都是光标的位置在哪就开始读哪. many_data=cur.fetchmay(4)#取出4条数据 #上面的fetch的结果都是元组类型的,没法看出哪个数据对应哪个字段,所以我们可以用字典,{'字段名':值} cursor=conn.cursor(currsor=pymysql.cursors.DictCursor) #此时获取的结果就是字典格式, #移动光标 currsor.scroll(数字,模式) 第一个参数是个int类型的数字,表示往后移动的记录的条数,
第二个参数是移动的模式,有两个值 : absolute :绝对移动, relative: 相对移动. 绝对移动:相对于所有数据的起始位置开始往后移动的 相对移动:相对于游标的当前位置开始往后移动的 #绝对移动的演示 #print(cursor.fetchall()) #cursor.scroll(3,'absolute') #从初始位置往后移动三条,那么下次取出的数据为第四条数据 #print(cursor.fetchone()) #相对移动的演示 #print(cursor.fetchone()) #cursor.scroll(1,'relative') #通过上面取了一次数据,游标的位置在第二条的开头,
我现在相对移动了1个记录,那么下次再取,取出的是第三条,我相对于上一条,往下移动了一条 #print(cursor.fetchone()) print(res) #一个数字 cursor.close() #关闭游标 conn.close() #关闭连接 

execute()之sql注入

我们来使用数据进行用户名和密码的认证操作

import pymysql

conn = pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='666',
    database='crm',
    charset='utf8'
)

cursor = conn.cursor(pymysql.cursors.DictCursor)
uname = input('请输入用户名:')
pword = input('请输入密码:')

sql = "select * from userinfo where username='%s' and password='%s';"%(uname,pword)

res = cursor.execute(sql) #res我们说是得到的行数,如果这个行数不为零,说明用户输入的用户名和密码存在,如果为0说名存在,你想想对不

print(res) #如果输入的用户名和密码错误,这个结果为0,如果正确,这个结果为1
if res:
    print('登陆成功')
else:
    print('用户名和密码错误!')

#通过上面的验证方式,比我们使用文件来保存用户名和密码信息的来进行验证操作要方便很多。

 但是上面的操作是有BUG的,如果将在输入用户名的地方输入一个 chao'空格然后--空格然后加上任意的字符串,就能够登陆成功,也就是只知道用户名的情况下,他就能登陆成功的情况:

uname = input('请输入用户名:')
pword = input('请输入密码:')

sql = "select * from userinfo where username='%s' and password='%s';"%(uname,pword)
print(sql)
res = cursor.execute(sql) #res我们说是得到的行数,如果这个行数不为零,说明用户输入的用户名和密码存在,如果为0说名存在,你想想对不

print(res) #如果输入的用户名和密码错误,这个结果为0,如果正确,这个结果为1
if res:
    print('登陆成功')
else:
    print('用户名和密码错误!')
#运行看结果:居然登陆成功
请输入用户名:chao' -- xxx
请输入密码:
select * from userinfo where username='chao' -- xxx' and password='';
1
登陆成功

我们来分析一下:
此时uname这个变量等于什么,等于chao' -- xxx,然后我们来看我们的sql语句被这个字符串替换之后
是个什么样子: select * from userinfo where username='chao' -- xxx' and password=''; 其中chao后面
的这个',在进行字符串替换的时候,我们输入的是chao',这个引号和前面的引号组成了一对,
然后后面--在sql语句里面是注释的意思,也就是说--后面的sql语句被注释掉了。也就是说,
拿到的sql语句是select * from userinfo where username='chao';
然后就去自己的数据库里面去执行了,发现能够找到对应的记录,因为有用户名为chao的记录,
然后他就登陆成功了,但是其实他连密码都不知道,只知道个用户名。。。,他完美的跳过了你的认证环节。

  正确的解决方案

uname = input('请输入用户名:') #输入的内容是:chao' -- xxx或者xxx' or 1=1 -- xxxxx
pword = input('请输入密码:')

sql = "select * from userinfo where username=%s and password=%s;"
print(sql)
res = cursor.execute(sql,[uname,pword]) #res我们说是得到的行数,如果这个行数不为零,说明用户输入的用户名和密码存在,如果为0说名存在,你想想对不

print(res) #如果输入的用户名和密码错误,这个结果为0,如果正确,这个结果为1
if res:
    print('登陆成功')
else:
    print('用户名和密码错误!')
#看结果:
请输入用户名:xxx' or 1=1 -- xxxxx
请输入密码:
select * from userinfo where username=%s and password=%s;
0
用户名和密码错误

  刚才问题的原因在于:

#1、sql注入之:用户存在,绕过密码
chao' -- 任意字符

#2、sql注入之:用户不存在,绕过用户与密码
xxx' or 1=1 -- 任意字符

解决方案的总结:

# 原来是我们对sql进行字符串拼接
# sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd)
# print(sql)
# res=cursor.execute(sql)

#改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了)
sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上
res=cursor.execute(sql,[user,pwd]) #pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。
View Code

增 删 改:conn.commit()

注意:sql语句不要自己拼接,要交给excute来拼接

import pymysql
#链接
conn=pymysql.connect(host='localhost',port='3306',user='root',password='123',database='crm',charset='utf8')
#游标
cursor=conn.cursor()

#执行sql语句
#part1
# sql='insert into userinfo(name,password) values("root","123456");'
# res=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数
# print(res)
# print(cursor.lastrowid) #返回的是你插入的这条记录是到了第几条了

#part2
# sql='insert into userinfo(name,password) values(%s,%s);'
# res=cursor.execute(sql,("root","123456")) #执行sql语句,返回sql影响成功的行数
# print(res)
#还可以进行更改操作:
#res=cursor.excute("update userinfo set username='taibaisb' where id=2")
#print(res) #结果为1
#part3
sql='insert into userinfo(name,password) values(%s,%s);'
res=cursor.executemany(sql,[("root","123456"),("lhf","12356"),("eee","156")]) #执行sql语句,返回sql影响成功的行数,一次插多条记录
print(res)
#上面的几步,虽然都有返回结果,也就是那个受影响的函数res,但是你去数据库里面一看,并没有保存到数据库里面,
conn.commit() #必须执行conn.commit,注意是conn,不是cursor,执行这句提交后才发现表中插入记录成功,没有这句,上面的这几步操作其实都没有成功保存。
cursor.close()
conn.close()

  查:fetchone ,fetchmany ,fetchall

import pymysql
#链接
conn=pymysql.connect(host='localhost',user='root',password='123',database='egon')
#游标
cursor=conn.cursor()

#执行sql语句
sql='select * from userinfo;'
rows=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数rows,将结果放入一个集合,等待被查询

# cursor.scroll(3,mode='absolute') # 相对绝对位置移动
# cursor.scroll(3,mode='relative') # 相对当前位置移动
res1=cursor.fetchone()
res2=cursor.fetchone()
res3=cursor.fetchone()
res4=cursor.fetchmany(2)
res5=cursor.fetchall()
print(res1)
print(res2)
print(res3)
print(res4)
print(res5)
print('%s rows in set (0.00 sec)' %rows)



conn.commit() #提交后才发现表中插入记录成功
cursor.close()
conn.close()

  

获取插入的最后一条数据的自增ID

import pymysql
conn=pymysql.connect(host='localhost',user='root',password='123',database='egon')
cursor=conn.cursor()

sql='insert into userinfo(name,password) values("xxx","123");'
rows=cursor.execute(sql)
print(cursor.lastrowid) #在插入语句后查看

conn.commit()

cursor.close()
conn.close()

  

 

posted @ 2019-01-20 20:31  想扣篮的小矮子  阅读(108)  评论(0编辑  收藏  举报