【问题标题】:Postgresql Query to select entry keys in bytea fieldPostgresql 查询以在 bytea 字段中选择条目键
【发布时间】:2020-10-27 04:31:34
【问题描述】:

我正在使用 Postgresql 数据库。 table is ticketlines 包含一个名为 attributes 的 bytea 字段

JSON 视图中的字段内容示例:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    <properties>
    <comment>Ticket Attributes</comment>
    <entry key="product.candiscount">true</entry>
    <entry key="product.categoryid">8410a5e3-fc31-4370-b7da-d8406d80ca97</entry>
    <entry key="product.kitchen">false</entry>
    <entry key="product.com">false</entry>
    <entry key="product.alwaysavailable">false</entry>
    <entry key="product.service">true</entry>
    <entry key="product.warranty">false</entry>
    <entry key="product.taxcategoryid">000</entry>
    <entry key="product.vprice">false</entry>
    <entry key="product.discounted">no</entry>
    <entry key="notes">006942</entry>
    <entry key="product.texttip"/>
    <entry key="product.nosc">0</entry>
    <entry key="product.promotionadded">false</entry>
    <entry key="product.name">Pawn Payout</entry>
    <entry key="product.code">pp</entry>
    <entry key="product.ispack">false</entry>
    <entry key="product.alias"/>
    <entry key="product.verpatrib">false</entry>
    <entry key="product.managestock">true</entry>
    </properties>

是否有一种简单的方法来查询此数据以在 SELECT 语句中报告(即;SELECT product.name, notes)?

我对此很接近:但显然结构错误: SELECT xpath('//properties:notes/text()', CAST(convert_from(attributes, 'UTF-8') AS XML)) FROM ticket='7abc41d9-0d7f-4187-bb81-e4139d7728bf'

结果是: 错误:无效的 XML 内容详细信息:第 2 行:StartTag:无效的元素名称 ^ *** 错误 *** 错误:无效的 XML 内容 SQL 状态:2200N 详细信息:第 2 行:StartTag:无效的元素名称

【问题讨论】:

  • “JSON”是错字吗?在我看来像 XML。
  • 它来自pglobeditor.exe,xml、json和文本模式都是这样的。该应用程序在 Java 中运行,我假设 JSON 是它的完成方式。
  • XML、JSON 和文本大不相同。
  • 这段文字,只是在这里展示bytea字段的内容,目的是有人协助查询字段外的数据,无意争论XML的布局差异,文本,杰森,或其他任何东西。
  • 那么答案是响亮的“不”。如果未指定数据的结构,则不能期望能够从中提取元素。

标签: xml postgresql bytea


【解决方案1】:

bytea 的内容似乎不是正确的 XML - 查看convert_from 结果。

使用您在问题中显示的 XML,正确的 XPath 查询将是

SELECT xpath('//entry[@key=''notes'']/text()', xml) FROM ...

这将获得“key”属性值为notes的“entry”元素的内容。

【讨论】:

    【解决方案2】:

    我确实根据您所写的内容重新创建了您的表,并成功完成了此查询:

    SELECT xpath('/properties/entry[@key=''notes'']/text()', CAST(convert_from(attributes, 'UTF-8') AS XML)) AS result FROM ticketlines where ticket  =1;
    

    虽然我没有在您的查询中重现您的错误消息。请检查您的 DB-Table 中票证 7abc41d9-0d7f-4187-bb81-e4139d7728bf 的真实内容是什么。

    【讨论】:

    • 谢谢。还是不行。我将查询运行为: SELECT xpath('/properties/entry[@key=''notes'']/text()', CAST(convert_from(attributes, 'UTF-8') AS XML)) AS result FROM ticketlines where票='7abc41d9-0d7f-4187-bb81-e4139d7728bf';结果:错误:无效的 XML 内容详细信息:第 2 行:StartTag:无效元素名称 ttp://java.sun.com/dtd/properties.dtd" rel="nofollow" target="_blank">java.sun.com/dtd/properties.dtd"> ^ ********** 错误 **********错误:无效的 XML 内容 SQL 状态:2200N 详细信息:第 2 行:StartTag:无效元素名称 ttp://java.sun.com/dtd/properties.dtd" rel="nofollow" target="_blank">java.sun.com/dtd/properties.dtd">
    • 你使用什么版本的 Postgres?我的是11.5。您可以通过 SELECT Version();有疑问时在 SQL 中。快速的互联网搜索产生了一篇 2010 年的帖子,其中包含与 Postgres 中的 DOCTYPE 类似的问题。
    • 版本 9.4.5,这里是该表备份的链接:drive.google.com/file/d/196LfpsClRXL64TXGkOh3F4Dn_7GwIEH1/…
    • 您的 postgres 版本 9.4.5 已过时。如果我将 docker 容器降级为 postgres:9.4.5 映像,我可以重现您的错误消息,否则我不能。
    • 谢谢。我会努力迁移到更新的版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2021-12-17
    相关资源
    最近更新 更多