{"id":27031,"date":"2024-11-24T09:39:18","date_gmt":"2024-11-24T01:39:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27031\/"},"modified":"2024-11-24T09:39:18","modified_gmt":"2024-11-24T01:39:18","slug":"%e4%bd%bf%e7%94%a8-python-%e8%bf%9e%e6%8e%a5-mysql-%e5%b9%b6%e6%a3%80%e7%b4%a2%e6%95%b0%e6%8d%ae%e6%97%b6%ef%bc%8c%e9%81%87%e5%88%b0%e6%8a%a5%e9%94%99%e8%af%a5%e5%a6%82%e4%bd%95-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27031\/","title":{"rendered":"\u4f7f\u7528 Python \u8fde\u63a5 MySQL \u5e76\u68c0\u7d22\u6570\u636e\u65f6\uff0c\u9047\u5230\u201c&#8221;\u201d\u62a5\u9519\u8be5\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173114930296643.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 Python \u8fde\u63a5 MySQL \u5e76\u68c0\u7d22\u6570\u636e\u65f6\uff0c\u9047\u5230\u201c&#8221;\u201d\u62a5\u9519\u8be5\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528 Python \u8fde\u63a5 MySQL \u5e76\u68c0\u7d22\u6570\u636e\u65f6\uff0c\u9047\u5230\u201c&#8221;\u201d\u62a5\u9519\u8be5\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 Python \u8fde\u63a5 MySQL \u5e76\u68c0\u7d22\u6570\u636e<\/strong><\/p>\n<p>\u60f3\u8981\u4ece MySQL \u6570\u636e\u5e93\u68c0\u7d22\u6570\u636e\uff0c\u5374\u9047\u5230\u4e86 &#8220;<\/p>\n<hr>\n<p>&#8221; \u62a5\u9519\uff1f <\/p>\n<p>\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u65b9\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<ol>\n<li> <strong>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757\uff1a<\/strong>\u9996\u5148\uff0c\u5bfc\u5165\u4e86 .connector \u6a21\u5757\uff0c\u7528\u4e8e\u4e0e MySQL \u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/li>\n<li> <strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff1a<\/strong>\u901a\u8fc7 mysql.connector.connect() \u51fd\u6570\u8fde\u63a5\u5230\u76ee\u6807\u6570\u636e\u5e93\uff0c\u6307\u5b9a\u4e3b\u673a\u540d\u6216 IP \u5730\u5740\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u3002<\/li>\n<li> <strong>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\uff1a<\/strong>\u6e38\u6807\u5bf9\u8c61\u7528\u4e8e\u6267\u884c\u67e5\u8be2\u548c\u5904\u7406\u7ed3\u679c\u3002\u901a\u8fc7\u8c03\u7528 cursor() \u65b9\u6cd5\u6765\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\u3002<\/li>\n<li> <strong>\u5b9a\u4e49\u67e5\u8be2\u5b57\u7b26\u4e32\uff1a<\/strong>\u5b9a\u4e49\u8981\u6267\u884c\u7684\u67e5\u8be2\u8bed\u53e5\uff0c\u4f8b\u5982 &#8220;SELECT yu, rt FROM tablename&#8221;\u3002<\/li>\n<li> <strong>\u6267\u884c\u67e5\u8be2\uff1a<\/strong>\u4f7f\u7528 execute() \u65b9\u6cd5\u6267\u884c\u67e5\u8be2\uff0c\u8be5\u65b9\u6cd5\u63a5\u53d7\u67e5\u8be2\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u3002<\/li>\n<li> <strong>\u83b7\u53d6\u7ed3\u679c\uff1a<\/strong>\u4f7f\u7528 fetchall() \u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u67e5\u8be2\u7ed3\u679c\u3002\u7ed3\u679c\u662f\u4e00\u4e2a\u5143\u7ec4\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7ec4\u4ee3\u8868\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u4e00\u884c\u3002<\/li>\n<li> <strong>\u5904\u7406\u7ed3\u679c\uff1a<\/strong>\u904d\u5386\u7ed3\u679c\u5143\u7ec4\uff0c\u5e76\u63d0\u53d6\u6240\u9700\u7684\u5217\u503c\u3002<\/li>\n<li> <strong>\u9519\u8bef\u5904\u7406\uff1a<\/strong>\u4f7f\u7528 try \u548c except \u8bed\u53e5\u6765\u5904\u7406\u67e5\u8be2\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u3002\u5982\u679c\u53d1\u751f\u9519\u8bef\uff0c\u5c06\u6253\u5370\u4e00\u4e2a\u5305\u542b\u9519\u8bef\u6d88\u606f\u7684\u5f02\u5e38\u5bf9\u8c61\u3002<\/li>\n<li> <strong>\u91ca\u653e\u8d44\u6e90\uff1a<\/strong>\u6700\u540e\uff0c\u4f7f\u7528 close() \u65b9\u6cd5\u5173\u95ed\u6e38\u6807\u5bf9\u8c61\u548c\u8fde\u63a5\u5bf9\u8c61\uff0c\u91ca\u653e\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 Python \u8fde\u63a5 MySQL \u5e76\u68c0\u7d22\u6570\u636e\u65f6\uff0c\u9047\u5230\u201c&#8221;\u201d\u62a5\u9519\u8be5\u5982\u4f55\u89e3\u51b3\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Python \u8fde\u63a5 MySQL \u5e76\u68c0\u7d22\u6570\u636e \u60f3\u8981\u4ece MySQL \u6570\u636e\u5e93\u68c0\u7d22\u6570\u636e\uff0c\u5374\u9047\u5230\u4e86 &#8220; &#8221; \u62a5\u9519\uff1f \u89e3\u51b3\u6b64\u95ee\u9898\u7684\u65b9\u6cd5\u5982\u4e0b\u6240\u793a\uff1a \u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757\uff1a\u9996\u5148\uff0c\u5bfc\u5165\u4e86 .connector \u6a21\u5757\uff0c\u7528\u4e8e\u4e0e MySQL \u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002 \u8fde\u63a5\u5230\u6570\u636e\u5e93\uff1a\u901a\u8fc7 mysql.connector.connect() \u51fd\u6570\u8fde\u63a5\u5230\u76ee\u6807\u6570\u636e\u5e93\uff0c\u6307\u5b9a\u4e3b\u673a\u540d\u6216 IP \u5730\u5740\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u3002 \u521b\u5efa\u6e38\u6807\u5bf9\u8c61\uff1a\u6e38\u6807\u5bf9\u8c61\u7528\u4e8e\u6267\u884c\u67e5\u8be2\u548c\u5904\u7406\u7ed3\u679c\u3002\u901a\u8fc7\u8c03\u7528 cursor() \u65b9\u6cd5\u6765\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\u3002 \u5b9a\u4e49\u67e5\u8be2\u5b57\u7b26\u4e32\uff1a\u5b9a\u4e49\u8981\u6267\u884c\u7684\u67e5\u8be2\u8bed\u53e5\uff0c\u4f8b\u5982 &#8220;SELECT yu, rt FROM tablename&#8221;\u3002 \u6267\u884c\u67e5\u8be2\uff1a\u4f7f\u7528 execute() \u65b9\u6cd5\u6267\u884c\u67e5\u8be2\uff0c\u8be5\u65b9\u6cd5\u63a5\u53d7\u67e5\u8be2\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u3002 \u83b7\u53d6\u7ed3\u679c\uff1a\u4f7f\u7528 fetchall() \u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u67e5\u8be2\u7ed3\u679c\u3002\u7ed3\u679c\u662f\u4e00\u4e2a\u5143\u7ec4\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7ec4\u4ee3\u8868\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u4e00\u884c\u3002 \u5904\u7406\u7ed3\u679c\uff1a\u904d\u5386\u7ed3\u679c\u5143\u7ec4\uff0c\u5e76\u63d0\u53d6\u6240\u9700\u7684\u5217\u503c\u3002 \u9519\u8bef\u5904\u7406\uff1a\u4f7f\u7528 try \u548c except \u8bed\u53e5\u6765\u5904\u7406\u67e5\u8be2\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u3002\u5982\u679c\u53d1\u751f\u9519\u8bef\uff0c\u5c06\u6253\u5370\u4e00\u4e2a\u5305\u542b\u9519\u8bef\u6d88\u606f\u7684\u5f02\u5e38\u5bf9\u8c61\u3002 \u91ca\u653e\u8d44\u6e90\uff1a\u6700\u540e\uff0c\u4f7f\u7528 close() \u65b9\u6cd5\u5173\u95ed\u6e38\u6807\u5bf9\u8c61\u548c\u8fde\u63a5\u5bf9\u8c61\uff0c\u91ca\u653e\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002 \u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 Python \u8fde\u63a5 MySQL \u5e76\u68c0\u7d22\u6570\u636e\u65f6\uff0c\u9047\u5230\u201c&#8221;\u201d\u62a5\u9519\u8be5\u5982\u4f55\u89e3\u51b3\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/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-27031","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27031","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=27031"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27031\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}