免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 44289 | 回复: 10

python访问mysql的存储过程怎么访问 [复制链接]

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
发表于 2016-07-31 18:03 |显示全部楼层
存储过程返回结果集,获得这个结果集,现在写法有异常。
存储结果本身没有问题,mysql里都好用

  1. #!/usr/bin/python
  2. import MySQLdb

  3. db = MySQLdb.Connect(host='localhost', user='root', passwd='123456', db='test')

  4. cursor = db.cursor()

  5. try:
  6.         args=(2,2000)
  7.         cursor.callproc("proc3",args) #call proc3(2,2000)
  8.         print "XX1"
  9.         for result in cursor.stored_results():
  10.                 print "XX2";
  11. except:
  12.         print "Bad"


  13. print "TEST"
  14. db.close()
复制代码
运行结果会产生这个异常

  1. XX1
  2. Bad
  3. TEST
  4. Exception _mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query') in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0xb6b81f6c>> ignored
复制代码

  1. create database test;
  2. use test;
  3. create table t ( a int, b int);
  4. insert into t(a,b) values (1,1000);
  5. insert into t(a,b) values (1,2000);
  6. insert into t(a,b) values (1,3000);
  7. insert into t(a,b) values (2,1000);
  8. insert into t(a,b) values (2,2000);
  9. insert into t(a,b) values (2,3000);
  10. insert into t(a,b) values (3,1000);
  11. insert into t(a,b) values (3,2000);
  12. insert into t(a,b) values (3,3000);
  13. delimiter //
  14. create procedure proc1()
  15. begin
  16. select a,b from t;
  17. end
  18. //
  19. create procedure proc2(in a_max int)
  20. begin
  21. select a,b from t where a<=a_max;
  22. end
  23. //
  24. create procedure proc3(in a_max int,in b_max int)
  25. begin
  26. select a,b from t where a<=a_max and b<=b_max;
  27. end
  28. //
  29. delimiter ;
复制代码

论坛徽章:
0
发表于 2016-08-01 12:14 |显示全部楼层
模块用错了,import mysql.connector   才能用cursor.stored_results()

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
发表于 2016-08-01 14:20 |显示全部楼层
lz66 发表于 2016-08-01 12:14
模块用错了,import mysql.connector   才能用cursor.stored_results()

我回去试试

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
发表于 2016-08-01 21:40 |显示全部楼层
lz66 发表于 2016-08-01 12:14
模块用错了,import mysql.connector   才能用cursor.stored_results()

我安装不了mysql-connector-python
但我已经安装了MYSQLdb
请问用MYSQLdb如何搞定这个存储过程?

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
发表于 2016-08-01 22:18 |显示全部楼层
lz66 发表于 2016-08-01 12:14
模块用错了,import mysql.connector   才能用cursor.stored_results()

我还是用MYSQLdb

  1. #!/usr/bin/python
  2. import MySQLdb

  3. db = MySQLdb.Connect(host='localhost', user='root', passwd='123456', db='test')

  4. cursor = db.cursor()

  5. try:
  6.         sql = "select a,b from t where a<=2 and b<=2000";
  7.         #sql = "call proc3(2,2000)";
  8.         cursor.execute(sql)
  9.         while 1:
  10.                 data = cursor.fetchall()
  11.                 if data:
  12.                         for row in data:
  13.                                 print "%s %s" % (row[0],row[1])
  14.                 else:
  15.                         break
  16. except:
  17.         print "Bad"


  18. print "TEST"
  19. db.close()
复制代码
还是一楼的数据库、存储过程
以上运行结果正常:

  1. 1 1000
  2. 1 2000
  3. 2 1000
  4. 2 2000
  5. TEST
复制代码
如果把sql="call proc(2,2000)"
也可以执行
但db.close()为什么就崩溃了呢?

  1. 1 1000
  2. 1 2000
  3. 2 1000
  4. 2 2000
  5. TEST
  6. Exception _mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query') in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0xb6bbbf4c>> ignored
复制代码
完全不懂python,请教这段怎么改?

论坛徽章:
0
发表于 2016-08-02 10:05 |显示全部楼层
我试了下,这样不会报错
cursor.close()
db.close()

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:09
发表于 2016-08-02 10:18 |显示全部楼层
本帖最后由 lolizeppelin 于 2016-08-02 10:21 编辑

你的TEST被打印出来了  ....

也就是说  call proc是非阻塞的

然后你关闭了数据库连接

自然就报错了

估计call完  游标还要fetchall一下

楼上的直接关闭游标也可以

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
发表于 2016-08-02 20:17 |显示全部楼层
lz66 发表于 2016-08-02 10:05
我试了下,这样不会报错
cursor.close()
db.close()

测试了,如果返回一个结果集的确没问题了,cursor.close()一下即可,下次再用再重新cursor=db.cursor()即可
但还有一个问题:如果存储过程返回多个结果集就不行了,以下代码只可以接收到第一个结果集。

  1. #!/usr/bin/python
  2. import MySQLdb

  3. db = MySQLdb.Connect(host='localhost', user='root', passwd='123456', db='test')
  4. cursor = db.cursor()

  5. try:
  6.         sql = "call proc_ddee33(0)";
  7.         cursor.execute(sql)
  8.         while 1:
  9.                 data = cursor.fetchall()
  10.                 if data:
  11.                         for row in data:
  12.                                 print "%s %s" % (row[0],row[1])
  13.                 else:
  14.                         break
  15. except:
  16.         print "Bad"

  17. print "TEST3"
  18. cursor.close()
  19. db.close()

复制代码

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 09:55:28IT运维版块每日发帖之星
日期:2016-07-29 06:20:00
发表于 2016-08-03 11:56 |显示全部楼层
用callproc(procname, args) 和 nextset()试试......
官方文档有说明:
Advances the cursor to the next result set, discarding the remaining rows in the current result set. If there are no additional result sets, it returns None; otherwise it returns a true value.

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
发表于 2016-08-03 19:13 |显示全部楼层
ning_lianjie 发表于 2016-08-03 11:56
用callproc(procname, args) 和 nextset()试试......
官方文档有说明:

终于完全明白了,这才是关键。
不需要cursor.close()

  1. #!/usr/bin/python
  2. import MySQLdb

  3. db = MySQLdb.Connect(host='localhost', user='root', passwd='123456', db='test')
  4. cursor = db.cursor()

  5. try:
  6.         sql = "call proc_ddee33(0)";
  7.         cursor.execute(sql)
  8.         while 1:
  9.                 data = cursor.fetchall()
  10.                 if data:
  11.                         for row in data:
  12.                                 print "%s %s" % (row[0],row[1])
  13.                 else:
  14.                         break
  15.                 cursor.nextset(); #要加这句
  16. except:
  17.         print "Bad"

  18. print "TEST3"
  19. #cursor.close()
  20. db.close()
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP