pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Graeme Gemmill <graeme@gemmill.name>
To: pgsql-sql@lists.postgresql.org
Subject: Passinf field name
Date: Sat, 3 Nov 2018 17:40:53 +0000
Message-ID: <12a51f95-7c81-a63d-7327-460044459007@gemmill.name> (raw)

Help please: Trying to construct a query where a field name has to be 
passed.

Database wbdata has a field “date timestamp with timezone”.

cursor.execute("SELECT * FROM wbdata ORDER BY date;")

works.

cursor.execute("SELECT * FROM wbdata WHERE date > %s", (starttime,))

works.

I read in your FAQs that I have to use psycopg2.sql to pass a field name 
into the query, so to select a subset of wbdata where date is > 
startdate and the output is sorted by date, I tried

cursor.execute(

sql.SQL("SELECT * FROM wbdata ORDER BY %s WHERE date > %%s" % 
[sql.Identifier("date")], [starttime]))


which gives me an error:

TypeError: __init__() takes 2 positional arguments but 3 were given

What is a correct formulation please?

Thank you

view thread (2+ messages)  latest in thread

Message-ID: <12a51f95-7c81-a63d-7327-460044459007@gemmill.name>
Permalink:  ../12a51f95-7c81-a63d-7327-460044459007@gemmill.name/
Also on:    postgresql.org/message-id/12a51f95-7c81-a63d-7327-460044459007@gemmill.name

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: graeme@gemmill.name, pgsql-sql@lists.postgresql.org
  Subject: Re: Passinf field name
  In-Reply-To: <12a51f95-7c81-a63d-7327-460044459007@gemmill.name>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox