Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- krace@hotbox /m/u/code> cat json_pg.py
- import psycopg2
- def run(stmt):
- cur = psycopg2.connect(database='test', user='postgres', password='password', host='localhost').cursor()
- cur.execute(stmt)
- result = cur.fetchall()
- print(list(result))
- stmt = 'select cast(row_to_json(row) as text) from (select book.id, book.name, book.author_id, author.name as author_name from book inner join author on book.author_id = author.id) row;'
- run(stmt)
- krace@hotbox /m/u/code> python json_pg.py
- [('{"id":1,"name":"War and Peace","author_id":1,"author_name":"Leo Tolstoy"}',),
- ('{"id":2,"name":"The Trial","author_id":2,"author_name":"Kafka"}',),
- ('{"id":3,"name":"The metamorphosis","author_id":2,"author_name":"Kafka"}',)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement