{"id":207570,"date":"2025-07-08T11:11:01","date_gmt":"2025-07-08T03:11:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207570\/"},"modified":"2025-07-08T11:11:01","modified_gmt":"2025-07-08T03:11:01","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-postgres-%e4%b8%ad%e5%90%91-json-%e5%ad%97%e6%ae%b5%e6%95%b0%e7%bb%84%e6%b7%bb%e5%8a%a0%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207570\/","title":{"rendered":"\u5982\u4f55\u5728 Postgres \u4e2d\u5411 json \u5b57\u6bb5\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u5728 Postgres \u4e2d\u5411 json \u5b57\u6bb5\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 18:00:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2aGolang\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u5982\u4f55\u5728 Postgres \u4e2d\u5411 json \u5b57\u6bb5\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5c06\u6570\u636e\u9644\u52a0\u5230\u5c5e\u4e8e postgres \u4e2d json \u5b57\u6bb5\u7684\u6570\u7ec4\u3002\u5728\u4f7f\u7528 pgadmin \u65f6\uff0c\u6211\u77e5\u9053\u4ee5\u4e0b\u67e5\u8be2\u6709\u6548\u3002 \u301c<\/p>\n<pre>update lesson set data =\n    jsonb_set (data, '{pages, 999999}', '{\"pagenum\": 2, \"pagetype\": \"voc\"}', true)\nwhere id = 2;<\/pre>\n<p>\u6211\u53ea\u662f\u60f3\u901a\u8fc7\u7528 go \u7f16\u5199\u7684\u5176\u4f59 api \u8ba9\u4e0a\u8ff0\u67e5\u8be2\u5de5\u4f5c\u3002\u6211\u6536\u5230\u4e00\u6761\u9519\u8bef\u6d88\u606f\u201cpq\uff1ajson \u7c7b\u578b\u7684\u8f93\u5165\u8bed\u6cd5\u65e0\u6548\u201d\u3002<\/p>\n<p>\u6211\u7684\u4ee3\u7801\u5982\u4e0b~<\/p>\n<pre>_, err := db.exec(`\n    update lessons set data =\n        jsonb_set (data, '{pages, 999999}','{\"pagenum\": $1, \"pagetype\": $2}', true) \n    where id = $3`,\n    pagenum, pagetype, id) \/\/ variable types are int string int<\/pre>\n<p>\u6211\u6000\u7591 postgres \u9a71\u52a8\u7a0b\u5e8f\u6ca1\u6709\u63d2\u5165 $ \u53c2\u6570\u3002\u5982\u679c\u6211\u5728\u6574\u4e2a\u67e5\u8be2\u4e2d\u4f7f\u7528 fmt.sprinf() \uff0c\u5b83\u4f1a\u8d77\u4f5c\u7528\uff0c\u4f46\u6211\u8bd5\u56fe\u907f\u514d sql \u6ce8\u5165\u653b\u51fb\uff0c\u5e76\u4e14\u5e0c\u671b\u5229\u7528 go sql \u5e93\u7684\u5185\u7f6e\u5b89\u5168\u63aa\u65bd\u3002<\/p>\n<p>\u4ec5\u4f9b\u53c2\u8003\uff0c\u6211\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b~ \u8bfe\u7a0b\u8868<\/p>\n<pre>lessons\n   id    int\n   data  jsonb<\/pre>\n<p>go \u7ed3\u6784\uff1a<\/p>\n<pre>type Lesson struct {\n    ID    int    `json:\"id\"`\n    Name  string  `json:\"name\"`\n    Pages []Page  `json:\"pages\"`\n}\n\ntype Page struct {\n    PageNum    int  `json:\"pageNum\"`\n    PageType   string `json:\"pageType\"`<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u4e0d\u80fd\u5728 postgres \u4e2d\u7684\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u67e5\u8be2\u53c2\u6570\u3002\u5c06\u6574\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u5355\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9 postgres\uff1a<\/p>\n<pre>str := fmt.sprintf('{\"pagenum\": %d, \"pagetype\": %q}', pagenum, pagetype)\n_, err := db.exec(`\n    update lessons set data =\n        jsonb_set (data, '{pages, 999999}', $1, true) \n    where id = $2`,\n    str, id) \/\/ variable types are int string int<\/pre>\n<p>\u6216\u8005\u4f7f\u7528\u5728\u670d\u52a1\u5668\u7aef\u6267\u884c\uff1a<\/p>\n<pre>_, err := db.Exec(`\n    UPDATE lessons SET data =\n        jsonb_set (data, '{pages, 999999}','{\"pageNum\": ' || $1 || ', \"pageType\": ' || $2 || '}', True) \n    WHERE id = $3`,\n    pageNum, pageType, id) \/\/ variable types are int string int<\/pre>\n<p>\u6700\u597d\/\u6700\u5b89\u5168\u7684\u53ef\u80fd\u662f\u7b2c\u4e00\u79cd\u65b9\u6cd5\uff0c\u5728\u5ba2\u6237\u7aef\u4e2d\u4f7f\u7528\u5b8c\u6574\u7684 json \u7f16\u7ec4\uff0c\u800c\u4e0d\u662f\u7b80\u5355\u7684 <code>fmt.sprintf<\/code>\u3002\u6211\u5c06\u5176\u4f5c\u4e3a\u7ec3\u4e60\u7559\u7ed9\u8bfb\u8005\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u5728 Postgres \u4e2d\u5411 json \u5b57\u6bb5\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207570","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207570"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207570\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}