Re: [psycopg] Parse record type into tuple
От | Rory Campbell-Lange |
---|---|
Тема | Re: [psycopg] Parse record type into tuple |
Дата | |
Msg-id | 20170905223347.i7sdm2xd7cjgp5oq@campbell-lange.net обсуждение исходный текст |
Ответ на | Re: [psycopg] Parse record type into tuple (Rory Campbell-Lange <rory@campbell-lange.net>) |
Список | psycopg |
Hi Dmytro I misread your question; sorry. You clearly know the difference betwreen a row/record and array. The only approach I can think of is => select * from (values (1,2)) x (a,b); a | b ---+--- 1 | 2 or this works suprisingly well => create type e_test as (x integer, y integer); CREATE TYPE => select (1, 2)::e_test; row ------- (1,2) (1 row) => select to_json((1, 2)::e_test); to_json --------------- {"x":1,"y":2} (1 row) In [54]: c.execute('select to_json((1, 2)::e_test);') In [55]: r = c.fetchone() In [56]: r Out[56]: Record(to_json={u'y': 2, u'x': 1}) On 05/09/17, Rory Campbell-Lange (rory@campbell-lange.net) wrote: > Hi Dmytro > > I think your query is using row query syntax, the same as > > => select row(1,2); > row > ------- > (1,2) > > in Postgres (which is not a common thing to want to do). > https://www.postgresql.org/docs/9.6/static/sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS > > If you want to select an array from Postgres, you need different syntax, > e.g. > > => select array[1,2]; > array > ------- > {1,2} > (1 row) > > https://www.postgresql.org/docs/9.6/static/arrays.htmlttps://www.postgresql.org/docs/9.2/static/sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS > > In psycopg2: > > In [1]: import psycopg2 > In [2]: conn = psycopg2.connect(dbname='template1', user='dbuser', password='xxx') > In [3]: c = conn.cursor() > In [5]: c.execute("select array[1,2] as ar") > In [6]: c.fetchone() > Out[6]: ([1, 2],) > > In [7]: from psycopg2.extras import NamedTupleCursor > In [8]: c = conn.cursor(cursor_factory=NamedTupleCursor) > In [9]: c.execute("select array[1,2] as ar") > In [10]: c.fetchone() > Out[10]: Record(ar=[1, 2]) > > > I put in the NamedTupleCursor example in there as I find it invaluable. > > Kind regards > Rory > > > > On 05/09/17, Dmytro Starosud (d.starosud@gmail.com) wrote: > > I do following interactions with DB: > > > > In [48]: conn = psycopg2.connect("dbname=... user=... password=...") > > In [49]: cur = conn.cursor() > > In [50]: cur.execute("select (1, 2)") > > In [51]: cur.fetchone() > > Out[51]: ('(1,2)',) > > > > Is it possible to get that tuple parsed into python tuple in the same way > > array works? > > > > In [55]: cur.execute("select array[1, 2]") > > In [56]: cur.fetchone() > > Out[56]: ([1, 2],) > > > > Looks like that can be done if I register composite type for that tuple. > > But I would like it to work with any tuple. > > > > Please assist. > > > > Thanks in advance! > > Dmytro > > > -- > Sent via psycopg mailing list (psycopg@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/psycopg
В списке psycopg по дате отправления: