Retrieving Out Params From a Stored Procedure With Python
2016-09-14 01:37
453 查看
http://www.rodneyoliver.com/blog/2013/08/08/retrieving-out-params-from-a-stored-procedure-with-python/
AUG 8TH, 2013I 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..)) |
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.
12 | 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] |
12 | cursor.execute("select @_DeleteUser_2") cursor.execute("select @_DeleteUser_3") #etc... result = cursor.fetchall() |
相关文章推荐
- [转]How to get return values and output values from a stored procedure with EF Core?
- [referrence]Retrieving Scalar Data from a Stored Procedure
- 【LeetCode with Python】 Remove Duplicates from Sorted Array
- Debugging a SQL Stored Procedure from inside SQL Server 2000 Query Analyzer
- Returning clob to varchar from a stored procedure
- Using Databases with Python -Musical Track Database (produce database from XML file)
- How can I get a String from HID device in Python with evdev?
- oracle call stored procedure with schema - PLS-00487 ORA-06550
- 【LeetCode with Python】 Remove Duplicates from Sorted List II
- 【LeetCode with Python】 Remove Nth Node From End of List
- [python]Python中from...import与import......as与with.....as
- Starting Out with Python
- Programmatically Retrieving a Stored Procedure's Parameters 获取Sql Server 存储过程参数
- Run DTS from stored procedure
- 【LeetCode with Python】Remove Duplicates from Sorted Array II
- Beginning Game Development with Python and Pygame: From Novice to Professional
- Calling Oracle 10g Stored Proc with Assoc Array from C# VS 2008
- (pbc lua 加入)c++_lua_Python with/without extension性能测试 (10万次SerializeToString & ParseFromString)
- 【LeetCode with Python】 Remove Duplicates from Sorted Array II
- Get Resultset from Oracle Stored procedure