Chinaunix

标题: python访问mysql的存储过程怎么访问 [打印本页]

作者: cjaizss    时间: 2016-07-31 18:03
标题: python访问mysql的存储过程怎么访问
存储过程返回结果集,获得这个结果集,现在写法有异常。
存储结果本身没有问题,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 ;
复制代码

作者: lz66    时间: 2016-08-01 12:14
模块用错了,import mysql.connector   才能用cursor.stored_results()
作者: cjaizss    时间: 2016-08-01 14:20
lz66 发表于 2016-08-01 12:14
模块用错了,import mysql.connector   才能用cursor.stored_results()

我回去试试
作者: cjaizss    时间: 2016-08-01 21:40
lz66 发表于 2016-08-01 12:14
模块用错了,import mysql.connector   才能用cursor.stored_results()

我安装不了mysql-connector-python
但我已经安装了MYSQLdb
请问用MYSQLdb如何搞定这个存储过程?
作者: cjaizss    时间: 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,请教这段怎么改?
作者: lz66    时间: 2016-08-02 10:05
我试了下,这样不会报错
cursor.close()
db.close()

作者: lolizeppelin    时间: 2016-08-02 10:18
本帖最后由 lolizeppelin 于 2016-08-02 10:21 编辑

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

也就是说  call proc是非阻塞的

然后你关闭了数据库连接

自然就报错了

估计call完  游标还要fetchall一下

楼上的直接关闭游标也可以
作者: cjaizss    时间: 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()

复制代码

作者: ning_lianjie    时间: 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.

作者: cjaizss    时间: 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()
复制代码

作者: lolizeppelin    时间: 2016-09-05 16:55
学习了原来 fetchall不能返回所有的 结果级




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2