pymysql module introduction

pymysql

We must learn how to operate the pymysql is used in python mysql program, in essence, it is a socket client, but the client is in the socket with python program, since it is the client socket should be how to use, is not connected to the server, and the server and communicate ah, let's learn about pymysql this module

installation

pip3 install pymysql

use

Through a login authentication, which is authentication username and password combination exists in the user table to use it pymysql

First, we create a user table called userinfo table, where a username (name) and password fields (password), and then store some data in the table.

Then write the following in our python script, we can complete our certified landing

import pymysql
user=input('用户名: ').strip()
pwd=input('密码: ').strip()

#链接,指定ip地址和端口,本机上测试时ip地址可以写localhost或者自己的ip地址或者127.0.0.1,然后你操作数据库的时候的用户名,密码,要指定你操作的是哪个数据库,指定库名,还要指定字符集。不然会出现乱码
conn=pymysql.connect(host='localhost',port=3306,user='root',password='123',database='student',charset='utf8') 

cursor=conn.cursor() #这就想到于mysql自带的那个客户端的游标mysql> 在这后面输入指令,回车执行

sql='select * from userinfo where name="%s" and password="%s"' %(user,pwd) 

res=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目,是个数字,是受sql语句影响到的记录行数
#all_data=cursor.fetchall()  #获取返回的所有数据,注意凡是取数据,取过的数据就没有了,结果都是元祖格式的
#many_data=cursor.fetchmany(3) #一下取出3条数据,
#one_data=cursor.fetchone()  #按照数据的顺序,一次只拿一个数据,下次再去就从第二个取了,因为第一个被取出去了,取一次就没有了,结果也都是元祖格式的
if res:
    print('登录成功')
else:
    print('登录失败')

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

sql injection

The so-called sql injection, that is, by writing some special symbols to our sql statement, sql statement so that our lost its original role. For example, we bypass the authentication mechanism and complete the registration without knowing a user name and password.

Based on an example, we look at how you do things sql injection.

The following is an example of the code above

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('用户名和密码错误!')

But we look at the following operations, if the input chao in a place to enter a user name 'box and then - in the case of a space and then add any string, will be able to log in successfully, that is, only know the user's name, he You can log in case of success:

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
登陆成功

Let's analyze:

此时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的记录,然后他就登陆成功了,但是其实他连密码都不知道,只知道个用户名。。。,他完美的跳过了你的认证环节。

Then we look at an example, once a user name and password directly do not know, but still be able to log in is successful:

请输入用户名:xxx' or 1=1 -- xxxxxx
请输入密码:
select * from userinfo where username='xxx' or 1=1 -- xxxxxx' and password='';
3
登陆成功

我们只输入了一个xxx' 加or 加 1=1 加 -- 加任意字符串
看上面被执行的sql语句你就发现了,or 后面跟了一个永远为真的条件,那么即便是username对不上,但是or后面的条件是成立的,也能够登陆成功。

Such a problem, how do we solve? Can be solved by the execute method pymysql provided us see an example:

之前我们的sql语句是这样写的:
sql = "select * from userinfo where username='%s' and password='%s';"%(uname,pword)

以后再写的时候,sql语句里面的%s左右的引号去掉,并且语句后面的%(uname,pword)这些内容也不要自己写了,按照下面的方式写
sql = "select * from userinfo where username=%s and password=%s;"
难道我们不传值了吗,不是的,我们通过下面的形式,在excute里面写参数:
#cursor.execute(sql,[uname,pword]) ,其实它本质也是帮你进行了字符串的替换,只不过它会将uname和pword里面的特殊字符给过滤掉。

看下面的例子:
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
用户名和密码错误!

commit method

When we perform CRUD actions by pymysql, the following need to commit to take effect.

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,执行这句提交后才发现表中插入记录成功,没有这句,上面的这几步操作其实都没有成功保存。

Guess you like

Origin www.cnblogs.com/zhufanyu/p/12129461.html