首页 文章

psycopg2选择结果与其他数组嵌套

提问于
浏览
0

我试图使用“psycopg2”从postgres获取JSON行 . 记录的 Value 就像[[]] . 获得正确的JSON结果,即 . []我必须去记录1 . 不知道为什么会这样 .

import psycopg2
import sys
import json

conn_string = "'host='localhost' dbname='postgres' user='postgres' password='password123'" 
con=psycopg2.connect(conn_string)
cur = con.cursor()
cur.execute("select json_agg(art) from (select a.*, (select json_agg(b) from (select * from pfstklist where pfportfolioid = a.pfportfolioid ) as b) as pfstklist, (select json_agg(c) from (select * from pfmflist where pfportfolioid = a.pfportfolioid ) as c) as pfmflist from pfmaindetail as a) art")
records = cur.fetchall()
print(records)     #This gives result [[{....},{...},{...}]]
records1=records[0]    
print(records1)   #This gives expected result [{....},{...},{...}]

1 回答

  • 0

    cur.fetchall() 为您提供记录列表 . 你可能想要 cur.fetchone() .

相关问题