{"id":27924,"date":"2024-11-24T11:11:29","date_gmt":"2024-11-24T03:11:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27924\/"},"modified":"2024-11-24T11:11:29","modified_gmt":"2024-11-24T03:11:29","slug":"mysql-%e6%8a%a5%e8%a1%a8%e5%b7%a5%e5%85%b7%e6%8a%a5%e9%94%99%ef%bc%9a-%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27924\/","title":{"rendered":"MySQL \u62a5\u8868\u5de5\u5177\u62a5\u9519\uff1a \u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173133417575328.jpg\" class=\"aligncenter\" title=\"MySQL \u62a5\u8868\u5de5\u5177\u62a5\u9519\uff1a \u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"MySQL \u62a5\u8868\u5de5\u5177\u62a5\u9519\uff1a \u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/> \u5982\u4f55\u89e3\u51b3\uff1f&#8221; \/&gt;<\/p>\n<p><strong> \u62a5\u8868\u5de5\u5177<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 mysql \u62a5\u8868\u5de5\u5177\u65f6\uff0c\u5076\u5c14\u4f1a\u9047\u5230\u4e00\u4e9b\u62a5\u9519\u4fe1\u606f\uff0c\u8fd9\u4e9b\u4fe1\u606f\u53ef\u80fd\u6666\u6da9\u96be\u61c2\uff0c\u96be\u4ee5\u7406\u89e3\u3002\u672c\u7bc7\u95ee\u7b54\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5e38\u89c1\u7684\u62a5\u9519\u4fe1\u606f\u4e4b\u4e00\uff1a<\/p>\n<hr>\n<p>\u3002 <\/p>\n<p>\u8be5\u62a5\u9519\u4fe1\u606f\u901a\u5e38\u51fa\u73b0\u5728  \u811a\u672c\u4e2d\uff0c\u5f53\u5411 mysql \u6267\u884c\u67e5\u8be2\u65f6\u629b\u51fa\u3002\u5176\u6839\u672c\u539f\u56e0\u662f\u7f3a\u5c11 import \u8bed\u53e5\uff0c\u672a\u6b63\u786e\u5f15\u5165 mysql \u8fde\u63a5\u5668\u6a21\u5757\u3002<\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ea\u9700\u5728\u811a\u672c\u5f00\u5934\u6dfb\u52a0\u4ee5\u4e0b import \u8bed\u53e5\uff1a<\/p>\n<pre>import mysql.connector<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u8bed\u53e5\u5c06\u5bfc\u5165 mysql \u8fde\u63a5\u5668\u6a21\u5757\uff0c\u4f7f\u60a8\u80fd\u591f\u4e0e mysql \u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5\u5e76\u6267\u884c\u67e5\u8be2\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 mysql \u8fde\u63a5\u5668\u6267\u884c\u67e5\u8be2\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5176\u4e2d\u89e3\u51b3\u4e86 <\/p>\n<hr>\n<p> \u62a5\u9519\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u6dfb\u52a0\u9002\u5f53\u7684 import \u8bed\u53e5\u5e76\u6b63\u786e\u8fde\u63a5\u5230 mysql \u6570\u636e\u5e93\uff0c\u60a8\u5c06\u80fd\u591f\u89e3\u51b3 <\/p>\n<hr>\n<p> \u62a5\u9519\u5e76\u6210\u529f\u6267\u884c mysql \u67e5\u8be2\u3002 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fMySQL \u62a5\u8868\u5de5\u5177\u62a5\u9519\uff1a&lt;hr&gt; \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>\u5982\u4f55\u89e3\u51b3\uff1f&#8221; \/&gt; \u62a5\u8868\u5de5\u5177 \u5728\u4f7f\u7528 mysql \u62a5\u8868\u5de5\u5177\u65f6\uff0c\u5076\u5c14\u4f1a\u9047\u5230\u4e00\u4e9b\u62a5\u9519\u4fe1\u606f\uff0c\u8fd9\u4e9b\u4fe1\u606f\u53ef\u80fd\u6666\u6da9\u96be\u61c2\uff0c\u96be\u4ee5\u7406\u89e3\u3002\u672c\u7bc7\u95ee\u7b54\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5e38\u89c1\u7684\u62a5\u9519\u4fe1\u606f\u4e4b\u4e00\uff1a \u3002 \u8be5\u62a5\u9519\u4fe1\u606f\u901a\u5e38\u51fa\u73b0\u5728 \u811a\u672c\u4e2d\uff0c\u5f53\u5411 mysql \u6267\u884c\u67e5\u8be2\u65f6\u629b\u51fa\u3002\u5176\u6839\u672c\u539f\u56e0\u662f\u7f3a\u5c11 import \u8bed\u53e5\uff0c\u672a\u6b63\u786e\u5f15\u5165 mysql \u8fde\u63a5\u5668\u6a21\u5757\u3002 \u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ea\u9700\u5728\u811a\u672c\u5f00\u5934\u6dfb\u52a0\u4ee5\u4e0b import \u8bed\u53e5\uff1a import mysql.connector \u767b\u5f55\u540e\u590d\u5236 \u6b64\u8bed\u53e5\u5c06\u5bfc\u5165 mysql \u8fde\u63a5\u5668\u6a21\u5757\uff0c\u4f7f\u60a8\u80fd\u591f\u4e0e mysql \u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5\u5e76\u6267\u884c\u67e5\u8be2\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 mysql \u8fde\u63a5\u5668\u6267\u884c\u67e5\u8be2\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5176\u4e2d\u89e3\u51b3\u4e86 \u62a5\u9519\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 [&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-27924","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27924","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=27924"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27924\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}