Skip site navigation (1) Skip section navigation (2)

Re: How to call Postgresql Array Function in Python

From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
To: Rehan Saleem <pk_rehan(at)yahoo(dot)com>
Cc: "psycopg(at)postgresql(dot)org" <psycopg(at)postgresql(dot)org>
Subject: Re: How to call Postgresql Array Function in Python
Date: 2012-03-09 12:41:35
Message-ID: CA+mi_8YM-JnGHPso8eY0=jScarzKTLhtqNLk_n4H1kVsxWfYOA@mail.gmail.com (view raw or flat)
Thread:
Lists: psycopg
On Fri, Mar 9, 2012 at 12:00 PM, Rehan Saleem <pk_rehan(at)yahoo(dot)com> wrote:
> hi ,
> i am trying to call the postgresql function which takes array of type
> integer which deletes the customer record from table . which is
>
> select delcustumer('{6}')  -- where 6 is the customer id
>
> Now i am trying to call this postgresql function in python and i dont know
> how to pass array in python here is my python code
>
> i have declared a argv called custid in my python file
>
> cust =  connection.db_rel ( "select delcustumer('{}')")
> return cust
>
> how can i pass my variable custid which is an element of array which
> postgresql function is taking to this function in python .
> i shall be very thankful to you

Psycopg converts lists of python objects into arrays of the matching
type: see <http://initd.org/psycopg/docs/usage.html#adapt-list>.

You can use cur.execute("select delcustomer(%s)", ([6],)), or maybe
even cur.callproc("delcustomer", ([6],)). Note that the second
argument is a tuple with one element (the list of ids), so you need
the extra comma.

If postgres complains about not being able to find a function with the
provided arguments, try explicit casting with something like:

    cur.execute("select delcustomer(%s::int[])", ([6],))

I don't know that a db_rel is.

-- Daniele

In response to

psycopg by date

Next:From: Pervez, SalmanDate: 2012-03-21 20:54:46
Subject: import error
Previous:From: Rehan SaleemDate: 2012-03-09 12:00:37
Subject: How to call Postgresql Array Function in Python

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group