- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Postgres 的 row_to_json() 函数将数据检索为 json 对象,以便像使用 python 字典一样处理结果。
conn = psycopg2.connect("<My_DB_DSN>")
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
query_sql = "SELECT row_to_json(row) FROM (SELECT id, name FROM products) row;"
cur.execute(query_sql)
results = cur.fetchall()
print(results)
这导致:
[ [{"id": 1, "name": "Bob"}],
[{"id": 2, "name": "Susan"}]
]
我期待这样的结果:
[ {"id": 1, "name": "Bob"},
{"id": 2, "name": "Susan"}
]
知道为什么我会得到第一个结果吗?我该如何解决这个问题?
在 postgres 的命令行中运行 SQL 查询将按预期返回 json:
{"id": 1, "name": "Bob"},
{"id": 2, "name": "Susan"}
最佳答案
我认为您需要 RealDictCursor
,它会将每一行作为字典返回,您不需要修改 SQL 查询:
from psycopg2.extras import RealDictCursor
cur = conn.cursor(cursor_factory=RealDictCursor)
query_sql = "SELECT id, name FROM products where id < 10"
cur.execute(query_sql)
results = cur.fetchall()
print(results)
返回:
[{'id': 2L, 'name': 'Foo'}, {'id': 4L, 'name': 'Bar'}, ...]
关于python - row_to_json 和 psycopg2.fetchall() 结果是列表中的列表而不是列表中的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51381312/
考虑 tableA其中有列 row_added_dttm类型 timestamp without timezone .实际值为2017-08-31 18:34:42.813175 . 在我执行下面的查
我正在尝试使用 PostgreSQL 9.2 中添加的 row_to_json() 函数将查询结果映射到 JSON。 我无法找出将连接行表示为嵌套对象(1:1 关系)的最佳方式 这是我尝试过的(设置代
我创建了一个自定义类型如下: create type my_type as (camelCasedIdentifier uuid, ...); 我正在使用此自定义类型 my_type 来定义 JSON
这个问题在这里已经有了答案: How to set correct attribute names to a json aggregated result with GROUP BY clause?
我们有一个表 Things,其中包含通过 thing.project_id 属于表 Projects 的实例。 当查询 Things 的实例时,我们将返回父 Project 记录的 json 表示形式
例如,我在 PostgreSQL 9.2 中使用以下函数将 rows 转换为 json select row_to_json(row(productid, product)) from gtab04;
我有以下 Postgres 查询: "SELECT \"responses\".\"index\", \"responses\".\"created_at\", ROUND(AVG(\"respons
Postgres 在创建 JSON 导出时错误地转义引号。请注意以下更新中的双引号... UPDATE models SET column='"hello"' WHERE id=1; COPY (SE
我想将记录集的行转换为 JSON,但不包括任何最终将在 JavaScript 中未定义的空条目。例如,假设我有包含条目的表 testdata id | prop1 (integer) | prop2
我需要将表行导出为 JSON 并将其存储在审核表中,随后由 .NET 应用程序读取并反序列化该表。 Postgres 中有一个函数可以执行此操作: row_to_json(..) 我的问题是 NULL
这是这个优秀问答的后续:13227142 . 我几乎必须做同样的事情(受 PostgreSQL 9.2 的限制)但我只使用一个表。因此,查询使用自连接(为了生成正确的 JSON 格式),这会导致重复的
我使用 Postgres 的 row_to_json() 函数将数据检索为 json 对象,以便像使用 python 字典一样处理结果。 conn = psycopg2.connect("") cur
我正在尝试编写一个存储过程,我将查询传递到一个存储过程,该存储过程将在内部调用 row_to_json 函数。我们有多个 DBMS(Oracle、SQL Server 和 postgres),并希望编
我在 python 中运行一个 Postgres 查询,它返回一个 JSON 对象数组。 db = SQLDB('postgres://postgres:*****@localhost:543
我有一个包含 3 列的表格: id|name|geometry ----------------- 1 |xie|geometry 我想获取 geojson 格式的几何图形。 select id,n
我在本地开发环境中编写了以下查询,没有意识到我本地开发环境中的 postgres 版本是 9.3+,但在生产环境中只有 9.1。我对升级生产 postgres 犹豫不决。有什么办法可以编写与 9.1
我有一个包含如下数据的表: col1 col2 col3 col4 json_data ------------------------------------
我在 postgres 9.3.5 中有一个表,看起来像这样: CREATE TABLE customer_area_node ( id bigserial NOT NULL, custome
我想弄清楚如何将 Postgres 的 (9.2) row_to_json 与 SqlAlchemy 一起使用。然而,我还没有想出任何有效的语法。 details_foo_row_q = select
我正在尝试使用 2 个表创建嵌套的 json 数组。 我有 2 个表 journal 和 journaldetail。 架构是 - 日志:journalid,总量 journaldetail : jo
我是一名优秀的程序员,十分优秀!