delimiter $$
create procedure p1(
in m int, # 只进不出 m不能返回出去
in n int,
out res int # 该形参可以返回出去,该值只能传变量名
)
begin
select name from student where id>m and id<n; -- sql查询语句
set res=666; # 将res变量修改 用来标识当前的存储过程代码确实执行了
end $$
delimiter ;
# 针对形参res 不能直接传数据 应该传一个变量名
# 定义变量
set @ret = 10; -- mysql定义变量的方式
# 查看变量对应的值
select @ret;
mysql> select @ret;
+------+
| @ret |
+------+
| 10 |
+------+
1 row in set (0.00 sec)
mysql> call p1(4,8,@ret); -- 调用存储过程
+------------+
| name |
+------------+
| 261工程 |
| T92 |
| Gw E系列 |
+------------+
3 rows in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> select @ret; -- 查看返回的值,确实被修改了,说明存储过程的sql语句确实执行了
+------+
| @ret |
+------+
| 666 |
+------+
1 row in set (0.00 sec)
import pymysql
conn = pymysql.connect(host = '127.0.0.1',port = 3306,user = 'root',password = '123456',db = 'db3',charset = 'utf8mb4')
# cursor = conn.cursor() # 默认元组返回值的形式
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor) # 设置返回类型为字典
cursor.callproc('p1',(4,8,10)) -- 使用py传值,返回的值不需要传变量,pymysql模块内部优化
# msg = cursor.fetchone() # 取出一个
# msg = cursor.fetchmany(2) # 取出指定个
msg = cursor.fetchall() # 取出全部
print(msg) # 这里才是真正查询的数据,需要cursor赋值给变量
# cursor.scroll(6, mode='absolute') # 相对绝对位置移动游标
# # cursor.scroll(2, mode='relative') # 相对当前位置移动游标
# print(cursor.fetchone(),'相对当前位置2,再次取值将取出第5个值,因为前面已经取了两次,游标2,下一个值就是5')
conn.commit() # 不要自己拼接传值,使用自带功能,需要调用commit()
cursor.close()
conn.close()
cursor.callproc('p1',(4,8,10))
'''
@_名_索引=值
@_p1_0=4
@_p1_1=8
@_p1_2=10 -- py模块实际传值
'''