基于pymysql模块的增删改查

上课笔记

重点:(熟练)
多表查询
创建存储过程
原生sql
索引原理


pymysql  封装好的客户端
cursor 底层就是一个send操作
commit 告诉mysql真的要完成修改操作(不然修改不会生效)
execute 执行
查询不需要commit

password(‘123’) 可以得到hash,mysql自带的

sql中--代表注释 后面都无效

sql注入 不需要账号,密码就可登陆网站了
跳过前端筛选,所以在应用程序也要在检测
怎么解决sql注入问题
popen 看一下

cursor.fetchall() 拿到查询的结果 从管道里拿值

ctrl /注释 ctrl shift /解除注释

视图不要改,只是为了方便查询,不需要重复连接

以后视图轻易不要用,查询有变动要联系别人DBA修改,很麻烦,不推荐用,自己的数据库可以用视图

delimiter 重新声明一下sql语句结束标志
结束后需要还原 delimiter;

transaction 事务,交易
但凡遇到转账,交易之类需要用

rollback 回滚 遇到commit回滚就是修改后的数据

存储过程 封装视图,触发器,函数等 直接调用存储过程名
无参存储过程
delimiter $$
create procedure p1()
BEGIN
......
END
delimiter ;


delimiter $$
create procedure p1()
BEGIN
declare n int default 1;
while (n<100) do
insert into s1 values(n,concat('egon',n),'male',concat('egon',n,'@163.com'));
set n = n+1;
end while;
......
END
delimiter ;

基于pymysql模块的增删改查

1.基于pymysql插入操作(有注释)
 1 import pymysql
 2 
 3 # 首先要建立和数据库的链接
 4 client=pymysql.connect(
 5     host='127.0.0.1',
 6     port=3306,
 7     user='root',
 8     password='egon123',
 9     database='db6',
10     charset='utf8'
11 )
12 
13 cursor1=client.cursor() # cursor是游标,执行结果默认返回元组
14 
15 sql='insert into t1 values(1,"egon");'  # 插入sql语句
16 try:
17     res=cursor1.execute(sql)  # 执行sql语句
18     print(res)
19     client.commit()  # 加上commit告诉mysql需要完成插入修改操作,不加插入不会生效。
20 except Exception: # 如果sql语句执行出现异常捕捉到就执行回滚操作,把执行成功的语句都撤销
21     client.rollback()
22 
23 cursor1.close() # 最后记得关闭游标
24 client.close()  # 关闭连接
基于pymysql执行插入操作

2.-- 是mysql语句中注释

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

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

sql注入 不需要账号,密码就可登陆网站了
现在网站不让输入特殊字符,就是因为这个原因。
跳过前端筛选,所以在应用程序也要在检测。
3.基于pymysql增删操作(有注释)

 1 import pymysql
 2 
 3 client=pymysql.connect(
 4     host='127.0.0.1',
 5     port=3306,
 6     user='root',
 7     password='egon123',
 8     database='db6',
 9     charset='utf8'
10 )
11 
12 cursor=client.cursor()
13 
14 sql='insert into t1 values(3,"alex"),(4,"lxx");'
15 userinfo=[
16     (3,"alex"),
17     (4,"lxx"),
18     (5,"yxx")
19 ]
20 for user in userinfo: # 插入操作
21     sql='insert into t1 values(%s,"%s");' %(user[0],user[1])
22     # print(sql)
23     cursor.execute(sql)  # 返回的是执行成功的行数
24 
25 sql='insert into t1 values(%s,%s);'
26 cursor.executemany(sql,userinfo)  # 插入多条记录,它的运行原理就是循环userinfo。
27 
28 cursor.execute('delete from t1 where id=3;')  # 删除操作
29 
30 client.commit()  # 这个想要修改成功必须要加
31 
32 cursor.close()
33 client.close()
基于pymysql增删操作

4.基于pymysql在数据库中进行查询

4.1 存在注入问题的登陆

 1 import pymysql
 2 
 3 client=pymysql.connect(
 4     host='127.0.0.1',
 5     port=3306,
 6     user='root',
 7     password='egon123',
 8     database='db6',
 9     charset='utf8'
10 )
11 
12 cursor=client.cursor()
13 #查询
14 inp_user=input('输入账号名: ').strip()
15 inp_pwd=input('输入密码: ').strip()
16 # 在sql语句里password(字符串) 执行hash操作,一般我们不用,都会调用hashlib来完成
17 # sql='select id from user where name = "%s" and pwd = password("%s");' %(inp_user,inp_pwd)
18 sql='select id from user where name = "%s" and pwd = "%s";' %(inp_user,inp_pwd)
19 print(sql)
20 rows=cursor.execute(sql)  # 返回的是行数,执行成功rows就不为0,if rows就为True
21 if rows: # rows返回值不为0,就显示登陆成功
22     print('\033[45m登陆成功\033[0m')
23 else:
24     print('\033[46m用户名或密码错误\033[0m')
25 
26 cursor.close() # 日常两个关闭操作
27 client.close()
存在注入问题

4.2 解决注入问题(有注释)

 1 import pymysql
 2 
 3 client=pymysql.connect(
 4     host='127.0.0.1',
 5     port=3306,
 6     user='root',
 7     password='egon123',
 8     database='db6',
 9     charset='utf8'
10 )
11 
12 cursor=client.cursor()
13 #查询
14 inp_user=input('输入账号名: ').strip()
15 inp_pwd=input('输入密码: ').strip()
16 
17 # 放到cursor.execute()里,sql语句会把%s自动识别为字符串,不需要再加引号
18 sql='select id from user where name = %s and pwd = %s;'
19 rows=cursor.execute(sql,(inp_user,inp_pwd))
20 if rows:
21     print('\033[45m登陆成功\033[0m')
22 else:
23     print('\033[46m用户名或密码错误\033[0m')
24 
25 cursor.close()
26 client.close()
解决注入问题

4.3 获取查询结果

 1 # 提交查询语句并且拿到查询结果
 2 import pymysql
 3 
 4 client=pymysql.connect(
 5     host='127.0.0.1',
 6     port=3306,
 7     user='root',
 8     password='egon123',
 9     database='db6',
10     charset='utf8'
11 )
12 
13 cursor=client.cursor(pymysql.cursors.DictCursor)
14 #查询
15 
16 sql='select * from user where id > 3'
17 rows=cursor.execute(sql)
18 print(rows)
19 print(cursor.fetchall())  # 获得所有的记录
20 print(cursor.fetchall())
21 
22 print(cursor.fetchone()) # 获得一个记录
23 print(cursor.fetchone())
24 print(cursor.fetchone())
25 
26 print(cursor.fetchmany(2)) # 获得多个记录,括号里可以指定记录
27 print(cursor.fetchone())
28 
29 
30 
31 
32 print(cursor.fetchall())
33 # cursor.scroll(0,mode='absolute') # 绝对位置移动
34 # cursor.scroll(1,mode='absolute') # 绝对位置移动
35 print(cursor.fetchall())
36 
37 
38 print(cursor.fetchone())
39 cursor.scroll(2,mode='relative') # 相对当前位置移动
40 print(cursor.fetchone())
41 
42 cursor.close()
43 client.close()
获取查询结果


猜你喜欢

转载自www.cnblogs.com/Roc-Atlantis/p/9362619.html