Re: how to pass in bind variables with cursor.copy_expert?

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Federico Di Gregorio <fog(at)dndg(dot)it>, "W(dot) Matthew Wilson" <matt(at)tplus1(dot)com>, "psycopg(at)postgresql(dot)org" <psycopg(at)postgresql(dot)org>
Subject: Re: how to pass in bind variables with cursor.copy_expert?
Date: 2014-02-21 18:54:05
Message-ID: 5307A0CD.30501@aklaver.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: psycopg

On 02/21/2014 08:47 AM, Federico Di Gregorio wrote:
> On 21/02/2014 17:27, W. Matthew Wilson wrote:
>> I need to extract some rows from a table as a CSV file, based on a
>> query sort of like:
>>
>> copy (
>> select *
>> from blah
>> where x = %(x)s
>> )
>>
>> but I don't see how to pass in a dictionary with values for the query!

Would something like this work, or am I missing something?:

sql = """COPY (SELECT * FROM cell_per WHERE cell_per = %(cp)s) TO
'/home/postgres/cp.csv'"""

cur.execute(sql,{"cp": 18})

>
> You can use cursor.mogrify() to build the query and then pass it to
> cursor.copy_export(). See the documentation for the details.
>
> federico
>

In response to

Browse psycopg by date

  From Date Subject
Next Message Adrian Klaver 2014-02-21 19:01:00 Re: how to pass in bind variables with cursor.copy_expert?
Previous Message Federico Di Gregorio 2014-02-21 16:47:02 Re: how to pass in bind variables with cursor.copy_expert?