pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedPassinf field name
2+ messages / 2 participants
[nested] [flat]
* Passinf field name
@ 2018-11-03 17:40 Graeme Gemmill <graeme@gemmill.name>
0 siblings, 1 reply; 2+ messages in thread
From: Graeme Gemmill @ 2018-11-03 17:40 UTC (permalink / raw)
To: pgsql-sql@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Passinf field name
@ 2018-11-03 17:47 Jonathan S. Katz <jonathan.katz@excoventures.com>
parent: Graeme Gemmill <graeme@gemmill.name>
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan S. Katz @ 2018-11-03 17:47 UTC (permalink / raw)
To: Graeme Gemmill <graeme@gemmill.name>; +Cc: pgsql-sql@lists.postgresql.org
> On Nov 3, 2018, at 1:40 PM, Graeme Gemmill <graeme@gemmill.name> wrote:
>
>
> 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?
If you’re just trying to order by the “date” column, this should work fine:
cursor.execute("SELECT * FROM wbdata WHERE date > %s ORDER BY date", (starttime,))
Best,
Jonathan
Attachments:
[application/pgp-signature] signature.asc (832B, ../../3CDAB737-C9F2-4F3E-A9EB-5A06DE6F2C9F@excoventures.com/3-signature.asc)
download
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2018-11-03 17:47 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-11-03 17:40 Passinf field name Graeme Gemmill <graeme@gemmill.name>
2018-11-03 17:47 ` Jonathan S. Katz <jonathan.katz@excoventures.com>
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