{"id":7752,"date":"2024-11-14T14:56:47","date_gmt":"2024-11-14T06:56:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7752\/"},"modified":"2024-11-14T14:56:47","modified_gmt":"2024-11-14T06:56:47","slug":"python%e8%bf%9e%e6%8e%a5mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e6%97%b6%e9%81%87%e5%88%b0%e6%8a%a5%e9%94%99%ef%bc%8c%e6%80%8e%e4%b9%88%e5%8a%9e%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7752\/","title":{"rendered":"Python\u8fde\u63a5MySQL\u6570\u636e\u5e93\u65f6\u9047\u5230\u62a5\u9519\uff0c\u600e\u4e48\u529e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173122005113457.jpg\" class=\"aligncenter\" title=\"Python\u8fde\u63a5MySQL\u6570\u636e\u5e93\u65f6\u9047\u5230\u62a5\u9519\uff0c\u600e\u4e48\u529e\uff1f\u63d2\u56fe\" alt=\"Python\u8fde\u63a5MySQL\u6570\u636e\u5e93\u65f6\u9047\u5230\u62a5\u9519\uff0c\u600e\u4e48\u529e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u51fa\u73b0\u62a5\u9519<\/strong><\/p>\n<p>\u5728python\u4e2d\uff0c\u4f7f\u7528mysql.connector\u8fde\u63a5mysql\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5982<\/p>\n<hr>\n<p>\u4e4b\u7c7b\u7684\u62a5\u9519\u3002 <\/p>\n<p><strong>\u95ee\u9898\u5206\u6790<\/strong><\/p>\n<p>\u6b64\u62a5\u9519\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\u9020\u6210\u7684\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>sql\u67e5\u8be2\u8bed\u53e5\u4e2d\u5b58\u5728\u8bed\u6cd5\u9519\u8bef<\/li>\n<li>\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u4e0d\u6b63\u786e<\/li>\n<li>\u6570\u636e\u5e93\u4e2d\u4e0d\u5b58\u5728\u88ab\u67e5\u8be2\u7684\u8868\u6216\u5b57\u6bb5<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li> <strong>\u4ed4\u7ec6\u68c0\u67e5sql\u67e5\u8be2\u8bed\u53e5\u3002<\/strong>\u786e\u4fdd\u8bed\u53e5\u8bed\u6cd5\u6b63\u786e\uff0c\u6240\u6709\u8868\u540d\u3001\u5b57\u6bb5\u540d\u548c\u5173\u952e\u5b57\u62fc\u5199\u6b63\u786e\u3002<\/li>\n<li> <strong>\u9a8c\u8bc1\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u3002<\/strong>\u68c0\u67e5\u6570\u636e\u5e93\u4e3b\u673a\u540d\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u662f\u5426\u6b63\u786e\u3002<\/li>\n<li> <strong>\u786e\u8ba4\u6240\u67e5\u8be2\u7684\u8868\u548c\u5b57\u6bb5\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u3002<\/strong>\u4f7f\u7528\u5176\u4ed6mysql\u5ba2\u6237\u7aef\uff08\u5982mysql workbench\uff09\u68c0\u67e5\u8868\u548c\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528mysql.connector\u6210\u529f\u8fde\u63a5mysql\u6570\u636e\u5e93\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>import mysql.connector\n\n# \u8fde\u63a5\u5230\u6570\u636e\u5e93\nconn = mysql.connector.connect(\n    host=\"\u4e3b\u673a\u540d\u6216IP\u5730\u5740\",\n    user=\"\u6570\u636e\u5e93\u7528\u6237\u540d\",\n    password=\"\u6570\u636e\u5e93\u5bc6\u7801\",\n    database=\"\u6570\u636e\u5e93\u540d\"\n)\n\n# \u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\nmycursor = conn.cursor()\n\n# \u5b9a\u4e49\u67e5\u8be2\u5b57\u7b26\u4e32\nquery_str = \"SELECT yu, rt FROM tablename\"\n\ntry:\n    # \u6267\u884c\u67e5\u8be2\n    mycursor.execute(query_str)\n    # \u83b7\u53d6\u7ed3\u679c\n    results = mycursor.fetchall()\n    # \u6253\u5370\u7ed3\u679c\n    for row in results:\n        yu, rt = row\n        print(f\"yu: {yu}, rt: {rt}\")\nexcept Exception as e:\n    # \u6253\u5370\u9519\u8bef\u4fe1\u606f\n    print(f\"An error occurred: {e}\")\nfinally:\n    # \u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\n    mycursor.close()\n    conn.close()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u786e\u4fdd\u5c06\u4e3b\u673a\u540d\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u66ff\u6362\u4e3a\u60a8\u7684\u5b9e\u9645\u503c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fPython\u8fde\u63a5MySQL\u6570\u636e\u5e93\u65f6\u9047\u5230\u62a5\u9519\uff0c\u600e\u4e48\u529e\uff1f\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>\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u51fa\u73b0\u62a5\u9519 \u5728python\u4e2d\uff0c\u4f7f\u7528mysql.connector\u8fde\u63a5mysql\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5982 \u4e4b\u7c7b\u7684\u62a5\u9519\u3002 \u95ee\u9898\u5206\u6790 \u6b64\u62a5\u9519\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\u9020\u6210\u7684\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b sql\u67e5\u8be2\u8bed\u53e5\u4e2d\u5b58\u5728\u8bed\u6cd5\u9519\u8bef \u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u4e0d\u6b63\u786e \u6570\u636e\u5e93\u4e2d\u4e0d\u5b58\u5728\u88ab\u67e5\u8be2\u7684\u8868\u6216\u5b57\u6bb5 \u89e3\u51b3\u65b9\u6848 \u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u4ed4\u7ec6\u68c0\u67e5sql\u67e5\u8be2\u8bed\u53e5\u3002\u786e\u4fdd\u8bed\u53e5\u8bed\u6cd5\u6b63\u786e\uff0c\u6240\u6709\u8868\u540d\u3001\u5b57\u6bb5\u540d\u548c\u5173\u952e\u5b57\u62fc\u5199\u6b63\u786e\u3002 \u9a8c\u8bc1\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u3002\u68c0\u67e5\u6570\u636e\u5e93\u4e3b\u673a\u540d\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u662f\u5426\u6b63\u786e\u3002 \u786e\u8ba4\u6240\u67e5\u8be2\u7684\u8868\u548c\u5b57\u6bb5\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u3002\u4f7f\u7528\u5176\u4ed6mysql\u5ba2\u6237\u7aef\uff08\u5982mysql workbench\uff09\u68c0\u67e5\u8868\u548c\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002 \u4ee3\u7801\u793a\u4f8b \u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528mysql.connector\u6210\u529f\u8fde\u63a5mysql\u6570\u636e\u5e93\u7684\u793a\u4f8b\u4ee3\u7801\uff1a import mysql.connector # \u8fde\u63a5\u5230\u6570\u636e\u5e93 conn = mysql.connector.connect( host=&#8221;\u4e3b\u673a\u540d\u6216IP\u5730\u5740&#8221;, user=&#8221;\u6570\u636e\u5e93\u7528\u6237\u540d&#8221;, password=&#8221;\u6570\u636e\u5e93\u5bc6\u7801&#8221;, database=&#8221;\u6570\u636e\u5e93\u540d&#8221; ) # \u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61 mycursor = conn.cursor() # \u5b9a\u4e49\u67e5\u8be2\u5b57\u7b26\u4e32 query_str = &#8220;SELECT yu, rt FROM tablename&#8221; try: # \u6267\u884c\u67e5\u8be2 mycursor.execute(query_str) # \u83b7\u53d6\u7ed3\u679c results = mycursor.fetchall() # \u6253\u5370\u7ed3\u679c for [&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-7752","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7752","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=7752"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7752\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}