{"id":207456,"date":"2025-07-08T09:48:02","date_gmt":"2025-07-08T01:48:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207456\/"},"modified":"2025-07-08T09:48:02","modified_gmt":"2025-07-08T01:48:02","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e5%9c%a8%e6%8f%92%e5%85%a5-postgres-%e5%90%8e%e6%88%91%e4%bc%9a%e6%94%b6%e5%88%b0-errnorowssql-no-rows-in-result-set-%ef%bc%8c%e5%8d%b3%e4%bd%bf%e6%8f%92%e5%85%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207456\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u5728\u63d2\u5165 postgres \u540e\u6211\u4f1a\u6536\u5230 ErrNoRows(&#8220;sql: no rows in result set&#8221;) \uff0c\u5373\u4f7f\u63d2\u5165\u5b9e\u9645\u4e0a\u6210\u529f\u4e86\uff1f"},"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>\u4e3a\u4ec0\u4e48\u5728\u63d2\u5165 postgres \u540e\u6211\u4f1a\u6536\u5230 ErrNoRows(&#8220;sql: no rows in result set&#8221;) \uff0c\u5373\u4f7f\u63d2\u5165\u5b9e\u9645\u4e0a\u6210\u529f\u4e86\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 19:21:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e3a\u4ec0\u4e48\u5728\u63d2\u5165 postgres \u540e\u6211\u4f1a\u6536\u5230 ErrNoRows(&#8220;sql: no rows in result set&#8221;) \uff0c\u5373\u4f7f\u63d2\u5165\u5b9e\u9645\u4e0a\u6210\u529f\u4e86\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u4f7f\u7528 golang-postgres\uff1a<\/p>\n<pre>\"database\/sql\"\n_ \"github.com\/lib\/pq\"<\/pre>\n<p><strong>\u6211\u5728\u505a\u4ec0\u4e48\uff1a<\/strong><\/p>\n<p>\u6211\u8fd0\u884c\u4e00\u4e2a\u9009\u62e9\u67e5\u8be2\u3002 \u5982\u679c\u6211\u5728\u9009\u62e9\u65f6\u6ca1\u6709\u6536\u5230\u4efb\u4f55\u6761\u76ee\uff0c\u6211\u4f1a\u7ee7\u7eed\u63d2\u5165\u4e00\u4e2a\u6761\u76ee\u3002 \u5426\u5219\uff0c\u66f4\u65b0\uff0c\u6216\u8005\u5176\u4ed6\u4ec0\u4e48&#8230;&#8230;<\/p>\n<p><strong>\u95ee\u9898\u662f<\/strong>\uff0c\u6bcf\u6b21 insert \u8fd4\u56de <strong>errnorows(&#8220;sql: no rows in result set&#8221;)<\/strong> \uff0c\u5373\u4f7f\u6211\u53ef\u4ee5\u5728\u6570\u636e\u5e93\u4e2d\u770b\u5230 insert \u6210\u529f\uff0c\u5e76\u6dfb\u52a0\u4e86\u884c\u3002<\/p>\n<p>\u6b64\u5916<strong>lastinsertid<\/strong>\u59cb\u7ec8\u4fdd\u6301\u4e3a0\uff0c\u800c\u5982\u679c\u6211\u68c0\u67e5\u6570\u636e\u5e93\uff0c\u5b83\u6709\u4e00\u4e2a\u5b9e\u9645\u503c\uff08\u4f8b\u5982\uff0c131\uff09<\/p>\n<p>\u4f3c\u4e4e\u4ee3\u7801\u4e2d\u4e0b\u9762<em>\u6ce8\u91ca 2<\/em> \u9644\u8fd1\u7684\u9519\u8bef\u5df2\u7f13\u5b58\u5728\u67d0\u5904\uff0c\u7136\u540e\u518d\u6b21\u663e\u793a\u3002<\/p>\n<pre>\/\/ 1. select first\nsquerystmt, spreperr := db.prepare(selectquery)\n    if spreperr != nil {\n        glog.v(3).infof(\"selectquery prepare failed. err: %v\", spreperr)\n        return spreperr\n    }\n\n    queryerr := squerystmt.queryrow(today, itemid).scan(&amp;a, &amp;b, &amp;c, &amp;d, &amp;e, &amp;f, &amp;g, &amp;h, &amp;i, &amp;j)\n    if queryerr != nil {\n\n\/\/ 2. getting errnorows here is expected, for the first entry\n        if queryerr == sql.errnorows {\n            glog.v(3).infof(\"selectquery returned no results. err: %v\", queryerr)\n\n            var lastinsertid int\n            iquerystmt, ipreperr := db.prepare(insertquery)\n            if ipreperr != nil {\n                glog.v(3).infof(\"insertquery prepare failed. err: %v\", ipreperr)\n                return ipreperr\n            }\n            inserterr := iquerystmt.queryrow(today, today, today, taskid).scan(&amp;lastinsertid)\n            if inserterr != nil {\n                \/\/ 3. somehow, even after row is correctly being created, i am getting inserterr as \"sql: no rows in result set\" here\n                glog.v(3).infof(\"insertquery failed. err: %v\", inserterr)\n            } else {\n\/\/ control never comes to this part.\n                glog.v(3).infof(\"insertquery successful. lastinsertid: %v\", lastinsertid)\n            }\n        } else {\n            glog.v(3).infof(\"couldn't run query on postgres database. err: %v\", queryerr)\n            return queryerr\n        }\n}<\/pre>\n<p>\u5728<strong>database\/sql<\/strong>\u5185\u90e8\u8c03\u8bd5\uff0c\u663e\u793a\u5b83\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u5757\u4e2d\u8fd4\u56de\u6b64\u9519\u8bef\uff1a<\/p>\n<pre>if !r.rows.next() {\n        if err := r.rows.err(); err != nil {\n            return err\n        }\n        return errnorows\n    }<\/pre>\n<p>\u4f46\u662f\u6bcf\u6b21\uff0c\u5982\u679c\u6211\u68c0\u67e5\u6570\u636e\u5e93\uff0c<strong>\u6570\u636e\u603b\u662f\u5df2\u7ecf\u63d2\u5165<\/strong>\u3002<\/p>\n<p><strong>\u7f16\u8f91\uff1a<\/strong><\/p>\n<pre>selectQuery = `select task_id, creation_date, times_x_ran,\n                        a_count, b_count, c_count, d_count, e_count \n                        from reports where creation_date = $1 and task_id = $2`\n\ninsertQuery = `insert into reports \n                (creation_date, creation_time, last_modified, task_id, times_x_ran, times_y_ran, a, b, c, d, e, x_errors, y_errors) \n                    values \n                    ($1, $2, $3, $4,\n                    1, 0, \n                    0, 0, 0, 0, 0, \n                    '', '' )`<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u5b9e\u9645\u4e0a\u95ee\u4e86\u4e24\u4e2a\u95ee\u9898\uff0c\u6240\u4ee5\u6211\u4f1a\u56de\u7b54\u4e24\u4e2a\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\n<p>\u5373\u4f7f\u63d2\u5165\u6210\u529f\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u662f<strong>ErrNoRows<\/strong>\uff1f<\/p>\n<p>\u8fd9\u662f\u9884\u671f\u7684\u884c\u4e3a\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u60c5\u51b5\u4e0b INSERT \u4e0d\u8fd4\u56de\u4efb\u4f55\u884c\u3002\u4f60\u7684\u201c\u9519\u8bef\u201d\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u95ee\u9898\u3002\u5982\u679c\u60a8\u5e0c\u671b INSERT \u8fd4\u56de\u884c\uff0c\u5219\u5fc5\u987b\u5728\u672b\u5c3e\u4f7f\u7528\u201cRETURNING\u201d\u5b50\u53e5\u3002<\/p>\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u201c\u7ed3\u679c\u96c6\u4e2d\u6ca1\u6709\u884c\u201d\u72b6\u6001\u662f\u901a\u8fc7\u9519\u8bef\u7684\u65b9\u5f0f\u4f20\u8fbe\u7684\uff0c\u8fd9\u53ef\u80fd\u662f\u60a8\u611f\u5230\u56f0\u60d1\u7684\u539f\u56e0\u3002\u4f46\u53ea\u6709\u5f53\u60a8\u671f\u671b\u884c\u65f6\uff0c\u8fd9\u624d\u5e94\u88ab\u89c6\u4e3a\u9519\u8bef\u3002<\/p>\n<\/li>\n<li>\n<p>\u6b64\u5916\uff0c<strong>lastInsertID<\/strong> \u59cb\u7ec8\u4fdd\u6301\u4e3a 0\u3002<\/p>\n<p>\u8fd9\u4e5f\u662f\u9884\u671f\u7684\u884c\u4e3a\u3002\u5982\uff1a<\/p>\n<p>pq\u4e0d\u652f\u6301database\/sql\u4e2dResult\u7c7b\u578b\u7684LastInsertId()\u65b9\u6cd5\u3002\u8981\u8fd4\u56de INSERT\uff08\u6216 UPDATE \u6216 DELETE\uff09\u7684\u6807\u8bc6\u7b26\uff0c\u8bf7\u5728\u6807\u51c6 Query \u6216 QueryRow \u8c03\u7528\u4e2d\u4f7f\u7528 Postgres RETURNING \u5b50\u53e5\uff1a<\/p>\n<\/li>\n<\/ol>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4e3a\u4ec0\u4e48\u5728\u63d2\u5165 postgres \u540e\u6211\u4f1a\u6536\u5230 ErrNoRows(&#8220;sql: no rows in result set&#8221;) \uff0c\u5373\u4f7f\u63d2\u5165\u5b9e\u9645\u4e0a\u6210\u529f\u4e86\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\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-207456","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207456","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=207456"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207456\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}