{"id":5516,"date":"2024-11-14T08:30:12","date_gmt":"2024-11-14T00:30:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5516\/"},"modified":"2024-11-14T08:30:12","modified_gmt":"2024-11-14T00:30:12","slug":"mysql%e8%be%93%e5%85%a5%e9%94%99%e8%af%af%e6%80%8e%e4%b9%88%e8%bf%94%e5%9b%9e","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5516\/","title":{"rendered":"mysql\u8f93\u5165\u9519\u8bef\u600e\u4e48\u8fd4\u56de"},"content":{"rendered":"<blockquote><p>\n  mysql \u901a\u8fc7\u9519\u8bef\u7801\u548c\u9519\u8bef\u6d88\u606f\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3002\u9519\u8bef\u7801\u8868\u793a\u7279\u5b9a\u9519\u8bef\u7c7b\u578b\uff0c\u9519\u8bef\u6d88\u606f\u63d0\u4f9b\u8be6\u7ec6\u4fe1\u606f\u3002\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u83b7\u53d6\u9519\u8bef\u4fe1\u606f\uff1a\u83b7\u53d6\u9519\u8bef\u7801\u548c\u6d88\u606f\uff1aselect last_insert_id()\uff1a\u83b7\u53d6\u63d2\u5165\u7684 idselect found_rows()\uff1a\u83b7\u53d6\u5339\u914d\u884c\u6570\u89e3\u91ca\u9519\u8bef\u7801\uff1a\u4ee3\u7801 1062\uff1a\u4e3b\u952e\u6216\u552f\u4e00\u7d22\u5f15\u51b2\u7a81\u4ee3\u7801 1064\uff1a\u8bed\u6cd5\u9519\u8bef\u4ee3\u7801 1146\uff1a\u8868\u6216\u5217\u4e0d\u5b58\u5728\u67e5\u627e\u9519\u8bef\u6d88\u606f\uff1a\u67e5\u770b mysql \u9519\u8bef\u4ee3\u7801\u6587\u6863\u4f7f\u7528 show errors \u547d\u4ee4\u4f7f\u7528 mysqlclient.error \u7c7b\uff08python\uff09\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052907242580191.jpg\" class=\"aligncenter\" title=\"mysql\u8f93\u5165\u9519\u8bef\u600e\u4e48\u8fd4\u56de\u63d2\u56fe\" alt=\"mysql\u8f93\u5165\u9519\u8bef\u600e\u4e48\u8fd4\u56de\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u9519\u8bef\u8f93\u5165\u8fd4\u56de<\/strong><\/p>\n<p><strong>\u9519\u8bef\u5982\u4f55\u8fd4\u56de\uff1f<\/strong><\/p>\n<p>MySQL \u4f1a\u901a\u8fc7\u9519\u8bef\u7801\u548c\u9519\u8bef\u6d88\u606f\u6765\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3002\u9519\u8bef\u7801\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u8868\u793a\u7279\u5b9a\u7c7b\u578b\u7684\u9519\u8bef\u3002\u9519\u8bef\u6d88\u606f\u5219\u63d0\u4f9b\u4e86\u6709\u5173\u9519\u8bef\u7684\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u8bf4\u660e<\/strong><\/p>\n<p><strong>1. \u83b7\u53d6\u9519\u8bef\u7801\u548c\u6d88\u606f<\/strong><\/p>\n<pre>SELECT LAST_INSERT_ID(); -- \u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u63d2\u5165\u7684 ID\nSELECT FOUND_ROWS(); -- \u83b7\u53d6\u5339\u914d\u884c\u7684\u6570\u91cf<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u89e3\u91ca\u9519\u8bef\u7801<\/strong><\/p>\n<ul>\n<li>\u9519\u8bef\u7801\u4e3a 1062\uff0c\u8868\u793a\u4e3b\u952e\u6216\u552f\u4e00\u7d22\u5f15\u51b2\u7a81\u3002<\/li>\n<li>\u9519\u8bef\u7801\u4e3a 1064\uff0c\u8868\u793a\u8bed\u6cd5\u9519\u8bef\u3002<\/li>\n<li>\u9519\u8bef\u7801\u4e3a 1146\uff0c\u8868\u793a\u8868\u6216\u5217\u4e0d\u5b58\u5728\u3002<\/li>\n<\/ul>\n<p><strong>3. \u67e5\u627e\u9519\u8bef\u6d88\u606f<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u67e5\u627e\u9519\u8bef\u6d88\u606f\uff1a<\/p>\n<ul>\n<li>\u67e5\u770b MySQL \u6587\u6863\u4e2d\u7684\u9519\u8bef\u4ee3\u7801\u5217\u8868\u3002<\/li>\n<li>\u4f7f\u7528 SHOW ERRORS \u547d\u4ee4\u83b7\u53d6\u8be6\u7ec6\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<li>\u5728 Python \u7b49\u7f16\u7a0b\u8bed\u8a00\u4e2d\u4f7f\u7528 client.Error \u7c7b\u83b7\u53d6\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<pre>-- \u5c1d\u8bd5\u63d2\u5165\u91cd\u590d\u7684\u4e3b\u952e\nINSERT INTO table1 (id, name) VALUES (1, 'John');\n-- \u83b7\u53d6\u9519\u8bef\u7801\nSELECT LAST_INSERT_ID(); -- \u8fd4\u56de 1062\n-- \u83b7\u53d6\u9519\u8bef\u6d88\u606f\nSHOW ERRORS; -- \u8f93\u51fa \"Duplicate entry '1' for key 'PRIMARY'\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u63d0\u793a<\/strong><\/p>\n<ul>\n<li>\u59cb\u7ec8\u68c0\u67e5 MySQL \u9519\u8bef\uff0c\u4ee5\u786e\u4fdd\u67e5\u8be2\u6216\u64cd\u4f5c\u5df2\u6210\u529f\u6267\u884c\u3002<\/li>\n<li>\u4f7f\u7528 SHOW ERRORS \u547d\u4ee4\u53ef\u4ee5\u83b7\u53d6\u66f4\u591a\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<li>\u4e86\u89e3\u5e38\u89c1\u7684\u9519\u8bef\u7801\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u8bca\u65ad\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u8f93\u5165\u9519\u8bef\u600e\u4e48\u8fd4\u56de\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql \u901a\u8fc7\u9519\u8bef\u7801\u548c\u9519\u8bef\u6d88\u606f\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3002\u9519\u8bef\u7801\u8868\u793a\u7279\u5b9a\u9519\u8bef\u7c7b\u578b\uff0c\u9519\u8bef\u6d88\u606f\u63d0\u4f9b\u8be6\u7ec6\u4fe1\u606f\u3002\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u83b7\u53d6\u9519\u8bef\u4fe1\u606f\uff1a\u83b7\u53d6\u9519\u8bef\u7801\u548c\u6d88\u606f\uff1aselect last_insert_id()\uff1a\u83b7\u53d6\u63d2\u5165\u7684 idselect found_rows()\uff1a\u83b7\u53d6\u5339\u914d\u884c\u6570\u89e3\u91ca\u9519\u8bef\u7801\uff1a\u4ee3\u7801 1062\uff1a\u4e3b\u952e\u6216\u552f\u4e00\u7d22\u5f15\u51b2\u7a81\u4ee3\u7801 1064\uff1a\u8bed\u6cd5\u9519\u8bef\u4ee3\u7801 1146\uff1a\u8868\u6216\u5217\u4e0d\u5b58\u5728\u67e5\u627e\u9519\u8bef\u6d88\u606f\uff1a\u67e5\u770b mysql \u9519\u8bef\u4ee3\u7801\u6587\u6863\u4f7f\u7528 show errors \u547d\u4ee4\u4f7f\u7528 mysqlclient.error \u7c7b\uff08python\uff09 MySQL \u9519\u8bef\u8f93\u5165\u8fd4\u56de \u9519\u8bef\u5982\u4f55\u8fd4\u56de\uff1f MySQL \u4f1a\u901a\u8fc7\u9519\u8bef\u7801\u548c\u9519\u8bef\u6d88\u606f\u6765\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3002\u9519\u8bef\u7801\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u8868\u793a\u7279\u5b9a\u7c7b\u578b\u7684\u9519\u8bef\u3002\u9519\u8bef\u6d88\u606f\u5219\u63d0\u4f9b\u4e86\u6709\u5173\u9519\u8bef\u7684\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\u3002 \u8be6\u7ec6\u8bf4\u660e 1. \u83b7\u53d6\u9519\u8bef\u7801\u548c\u6d88\u606f SELECT LAST_INSERT_ID(); &#8212; \u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u63d2\u5165\u7684 ID SELECT FOUND_ROWS(); &#8212; \u83b7\u53d6\u5339\u914d\u884c\u7684\u6570\u91cf \u767b\u5f55\u540e\u590d\u5236 2. \u89e3\u91ca\u9519\u8bef\u7801 \u9519\u8bef\u7801\u4e3a 1062\uff0c\u8868\u793a\u4e3b\u952e\u6216\u552f\u4e00\u7d22\u5f15\u51b2\u7a81\u3002 \u9519\u8bef\u7801\u4e3a 1064\uff0c\u8868\u793a\u8bed\u6cd5\u9519\u8bef\u3002 \u9519\u8bef\u7801\u4e3a 1146\uff0c\u8868\u793a\u8868\u6216\u5217\u4e0d\u5b58\u5728\u3002 3. \u67e5\u627e\u9519\u8bef\u6d88\u606f \u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u67e5\u627e\u9519\u8bef\u6d88\u606f\uff1a \u67e5\u770b MySQL \u6587\u6863\u4e2d\u7684\u9519\u8bef\u4ee3\u7801\u5217\u8868\u3002 \u4f7f\u7528 SHOW ERRORS \u547d\u4ee4\u83b7\u53d6\u8be6\u7ec6\u9519\u8bef\u4fe1\u606f\u3002 \u5728 Python \u7b49\u7f16\u7a0b\u8bed\u8a00\u4e2d\u4f7f\u7528 client.Error [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5516","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5516"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5516\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}