{"id":45507,"date":"2024-12-01T10:23:27","date_gmt":"2024-12-01T02:23:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/45507\/"},"modified":"2024-12-01T10:23:27","modified_gmt":"2024-12-01T02:23:27","slug":"python-mysql-connector-%e6%8a%a5%e9%94%99%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%e6%9f%a5%e8%af%a2%e8%af%ad%e6%b3%95%e9%94%99%e8%af%af%ef%bc%9f-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/45507\/","title":{"rendered":"Python MySQL Connector \u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u67e5\u8be2\u8bed\u6cd5\u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Python MySQL Connector \u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u67e5\u8be2\u8bed\u6cd5\u9519\u8bef\uff1f<\/h1>\n<p>\u7c73\u4e91\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/17313734146732a9665e4a5.jpg\" class=\"aligncenter\" title=\"Python MySQL Connector \u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u67e5\u8be2\u8bed\u6cd5\u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"Python MySQL Connector \u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u67e5\u8be2\u8bed\u6cd5\u9519\u8bef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>mysql connector \u62a5\u9519: \u5982\u4f55\u89e3\u51b3\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 python \u7684 mysql connector \u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u9519\u8bef\u3002\u5176\u4e2d\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u662f &#8220;<\/p>\n<hr>\n<p>\u201d\u3002\u8fd9\u662f\u56e0\u4e3a\u5f53\u60a8\u5c1d\u8bd5\u6267\u884c\u4e00\u4e2a\u67e5\u8be2\u65f6\uff0c\u67e5\u8be2\u5b57\u7b26\u4e32\u4e2d\u5b58\u5728\u8bed\u6cd5\u9519\u8bef\u3002 <\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u6b64\u9519\u8bef\uff0c\u8bf7\u68c0\u67e5\u60a8\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u9057\u6f0f\u6216\u4e0d\u5339\u914d\u7684\u5f15\u53f7\u6216\u62ec\u53f7<\/li>\n<li>\u5173\u952e\u5b57\u62fc\u5199\u9519\u8bef<\/li>\n<li>\u8868\u6216\u5217\u540d\u62fc\u5199\u9519\u8bef<\/li>\n<\/ul>\n<p>\u6b64\u5916\uff0c\u786e\u4fdd\u60a8\u7684 mysql connector \u5b89\u88c5\u5df2\u66f4\u65b0\uff0c\u5e76\u4e14\u60a8\u4f7f\u7528\u7684\u662f\u6700\u65b0\u7248\u672c\u3002<\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u6b63\u786e\u4f7f\u7528 mysql connector \u6267\u884c\u67e5\u8be2\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    mycursor.execute(query_str)\n    results = mycursor.fetchall()\n    for row in results:\n        yu, rt = row\n        print(f\"yu: {yu}, rt: {rt}\")\nexcept Exception as e:\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>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Python MySQL Connector \u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u67e5\u8be2\u8bed\u6cd5\u9519\u8bef\uff1f \u7c73\u4e91\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01 mysql connector \u62a5\u9519: \u5982\u4f55\u89e3\u51b3\uff1f \u5728\u4f7f\u7528 python \u7684 mysql connector \u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u9519\u8bef\u3002\u5176\u4e2d\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u662f &#8220; \u201d\u3002\u8fd9\u662f\u56e0\u4e3a\u5f53\u60a8\u5c1d\u8bd5\u6267\u884c\u4e00\u4e2a\u67e5\u8be2\u65f6\uff0c\u67e5\u8be2\u5b57\u7b26\u4e32\u4e2d\u5b58\u5728\u8bed\u6cd5\u9519\u8bef\u3002 \u4e3a\u4e86\u89e3\u51b3\u6b64\u9519\u8bef\uff0c\u8bf7\u68c0\u67e5\u60a8\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4ee5\u4e0b\u95ee\u9898\uff1a \u9057\u6f0f\u6216\u4e0d\u5339\u914d\u7684\u5f15\u53f7\u6216\u62ec\u53f7 \u5173\u952e\u5b57\u62fc\u5199\u9519\u8bef \u8868\u6216\u5217\u540d\u62fc\u5199\u9519\u8bef \u6b64\u5916\uff0c\u786e\u4fdd\u60a8\u7684 mysql connector \u5b89\u88c5\u5df2\u66f4\u65b0\uff0c\u5e76\u4e14\u60a8\u4f7f\u7528\u7684\u662f\u6700\u65b0\u7248\u672c\u3002 \u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u6b63\u786e\u4f7f\u7528 mysql connector \u6267\u884c\u67e5\u8be2\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; [&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-45507","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/45507","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=45507"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/45507\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=45507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=45507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=45507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}