http://www.rodneyoliver.com/blog/2013/08/08/retrieving-out-params-from-a-stored-procedure-with-python/

AUG 8TH, 2013

I was hacking some python today which calls a stored procedure that sends back an out parameter to indicate whether or not it completed is task succesfully. Now, calling a stored procedure in python is pretty straight forward

1
cursor.callproc("StoredProcName", (param1, param2, etc..))

I needed to grab the out parameter in my code and execute some logic based on the response returned. I’m a python noob and suprisingly I didn’t find any thing that really gave a good example. I found a few posts on Stack Overflow like this and this. Which gave me some clues. Then I found a post that led me to the mysql-python documentation about cursor objects. When I read the following snippet it clicked for me

callproc(procname, args)

Calls stored procedure procname with the sequence of arguments in args. Returns the original arguments. Stored procedure support only works with MySQL-5.0 and newer.

Compatibility note: PEP-249 specifies that if there are OUT or INOUT parameters, the modified values are to be returned. This is not consistently possible with MySQL. Stored procedure arguments must be passed as server variables, and can only be returned with a SELECT statement. Since a stored procedure may return zero or more result sets, it is impossible for MySQLdb to determine if there are result sets to fetch before the modified parmeters are accessible.

The parameters are stored in the server as @procnamen, where n is the position of the parameter. I.e., if you cursor.callproc(‘foo’, (a, b, c)), the parameters will be accessible by a SELECT statement as @foo_0, @foo_1, and @_foo_2.

Compatibility note: It appears that the mere act of executing the CALL statement produces an empty result set, which appears after any result sets which might be generated by the stored procedure. Thus, you will always need to use nextset() to advance result sets

The key part for me was:

The parameters are stored in the server as @procnamen, where n is the position of the parameter. I.e., if you cursor.callproc(‘foo’, (a, b, c)), the parameters will be accessible by a SELECT statement as @foo_0, @foo_1, and @_foo_2.

So what I need to do is perform an cursor.execute on the server variable @_procname_n. The results are tuples so accessing their values should be as simple as result[0]. Here is what I came up with. This is acutal code from the project I’m working on so I know it works.

1
2
3
4
5
6
7
8
9
10
 cursor.callproc("DeleteUser", (user[0].rstrip(), out_error))

 # This is how we have to get the out params in python.  See PEP-249
cursor.execute("select @_DeleteUser_1")
result = cursor.fetchall() if result[0]: #in this case a non-null response denotes a problem "user profile inavlid"
print "Not Found: ", user[0].rstrip(), "\t", user[1]
else:
print user[0].rstrip(), "\t", user[1]

If I need to get more out parameters I just do this:

1
2
3
4
cursor.execute("select @_DeleteUser_2")
cursor.execute("select @_DeleteUser_3")
#etc...
result = cursor.fetchall()

I’m sure there is a more elegant “pythonic” way to do this but its cool to figure it out yourself and its a great learning experience.

Retrieving Out Params From a Stored Procedure With Python的更多相关文章

  1. Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python

    f you are not familiar with MySQL stored procedures or want to review it as a refresher, you can fol ...

  2. EF 接收OUTPUT参数的方法 How to Retrieve Stored Procedure Output Parameters in Entity Framework

    原文地址:http://blogs.microsoft.co.il/gilf/2010/05/09/how-to-retrieve-stored-procedure-output-parameters ...

  3. [转]How to get return values and output values from a stored procedure with EF Core?

    本文转自:https://stackoverflow.com/questions/43935345/how-to-get-return-values-and-output-values-from-a- ...

  4. SQL Server 在多个数据库中创建同一个存储过程(Create Same Stored Procedure in All Databases)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 遇到的问题(Problems) 实现代码(SQL Codes) 方法一:拼接SQL: 方法二: ...

  5. Stored Procedure 里的 WITH RECOMPILE 到底是干麻的?

    在 SQL Server 创建或修改「存储过程(stored procedure)」时,可加上 WITH RECOMPILE 选项,但多数文档或书籍都写得语焉不详,或只解释为「每次执行此存储过程时,都 ...

  6. [转]Dynamic SQL & Stored Procedure Usage in T-SQL

    转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...

  7. [原] XAF How to bind a stored procedure to a ListView in XAF

    First, I suggest that you review the following topic to learn how to show a custom set of objects in ...

  8. Modify a Stored Procedure using SQL Server Management Studio

    In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand  ...

  9. Difference between Stored Procedure and Function in SQL Server

    Stored Procedures are pre-compile objects which are compiled for first time and its compiled format ...

随机推荐

  1. Vue.js2.0从入门到放弃---入门实例

    最近,vue.js越来越火.在这样的大浪潮下,我也开始进入vue的学习行列中,在网上也搜了很多教程,按着教程来做,也总会出现这样那样的问题(坑啊,由于网上那些教程都是Vue.js 1.x版本的,现在用 ...

  2. Node.js与Sails~方法拦截器policies

    回到目录 policies sails的方法拦截器类似于.net mvc里的Filter,即它可以作用在controller的action上,在服务器响应指定action之前,对这个action进行拦 ...

  3. 基础才是重中之重~ConcurrentDictionary让你的多线程代码更优美

    回到目录 ConcurrentDictionary是.net4.0推出的一套线程安全集合里的其中一个,和它一起被发行的还有ConcurrentStack,ConcurrentQueue等类型,它们的单 ...

  4. lua如何构造类

    function class(super, autoConstructSuper) local classType = {}; classType.autoConstructSuper = autoC ...

  5. DOM (Document Object Model)文档对象模型

    [理解下DOM] DOM——Document Object Mode.DOM是网页上XHTML中文档正文标题啊.段落.列表.样式.以及ID/class等所有其他数据的一个内部表示.我自己的理解是将网页 ...

  6. Atititi tesseract使用总结

    Atititi tesseract使用总结 消除bug,优化,重新发布.当前版本为3.02 项目下载地址为:http://code.google.com/p/tesseract-ocr. Window ...

  7. rabbitmq消息队列——"topic型交换器"

    在之前的章节中我们改进了我们的日志系统,我们使用direct型交换器代替了只能盲目广播消息的fanout型交换器,这使得我们可以有选择性地接收日志. 尽管使用direct型交换器改进了我们的日志系统, ...

  8. React(二)实现双向数据流

    <div id="app"></div> <script src="bower_components/react/react.min.js& ...

  9. KnockoutJS 3.X API 第三章 计算监控属性(5) 参考手册

    计算监控属性构造参考 计算监控属性可使用以下形式进行构造: ko.computed( evaluator [, targetObject, options] ) - 这种形式是创建一个计算监控属性最常 ...

  10. mongodb php

    首先安装扩展,然后才能使用mongodb 一.连接数据库 try { $mongo = new MongoClient(); $db = $mongo->mydb; var_dump($db); ...