0

I have one stored procedure in postgres which used out parameter. How to use that out parameter value in python.

create or replace procedure test(id number,result out varchar2)
as
begin
result := 'Saud';
end;

I want call above stored procedure from python.

How to call that procedure from python with out parameter?

1
  • Class assignment? It's OK if so, but please say so. Commented Apr 10, 2015 at 1:10

2 Answers 2

1

Consider a procedure like this

create or replace procedure test(in one integer, in two integer, inout three integer)
language plpgsql
as
$$
begin
three:=one+two;
end
$$

In order to execute this procedure with psycopg2 use the following format.

conn = psycopg2.connect("dbname='yourdbname' user='youruser' host='yourhostname' 
password='yourpassword'")
cursor=conn.cursor()
cursor.execute("call public.test(%s,%s,%s)",(1,2,three))
result = cursor.fetchone()
print(result[0])

This should print 3. The thing is that you must bind all procedure parameters. Hope this help you....

Sign up to request clarification or add additional context in comments.

Comments

0

Use psycopg2 to:

  • connect to the database

    conn = psycopg2.connect("dbname=mydb")
    
  • create a cursor

    curs = conn.cursor()
    
  • execute SQL like:

    curs.execute("SELECT test(%s)", (1,))
    
  • Fetch the result

    row = curs.fetchone()
    print row[0]
    

See the psycopg2 documentation and tutorial for details. You should also read the Python DB-API documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.