{"id":96998,"date":"2024-10-20T14:08:17","date_gmt":"2024-10-20T06:08:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96998\/"},"modified":"2024-10-20T14:08:18","modified_gmt":"2024-10-20T06:08:18","slug":"python%e7%a8%8b%e5%ba%8f%e5%85%a5%e9%96%80%e5%bf%85%e5%82%99%ef%bc%8c30%e7%a7%92%e5%ad%b8%e6%9c%83pymysql%e9%8f%88%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-pymysql%e9%8f%88%e6%8e%a5%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96998\/","title":{"rendered":"Python\u7a0b\u5e8f\u5165\u9580\u5fc5\u5099\uff0c30\u79d2\u5b78\u6703pymysql\u93c8\u63a5\u6578\u64da\u5eab (pymysql\u93c8\u63a5\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python%e7%a8%8b%e5%ba%8f%e5%85%a5%e9%96%80%e5%bf%85%e5%82%99%ef%bc%8c30%e7%a7%92%e5%ad%b8%e6%9c%83pymysql%e9%8f%88%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-SFehERkvsI\">Python\u7a0b\u5e8f\u5165\u9580\u5fc5\u5099\uff0c30\u79d2\u5b78\u6703pymysql\u93c8\u63a5\u6578\u64da\u5eab<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0cPython\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u4e14\u6613\u65bc\u5b78\u7fd2\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5df2\u7d93\u6210\u70ba\u8a31\u591a\u958b\u767c\u8005\u7684\u9996\u9078\u3002\u7121\u8ad6\u662f\u6578\u64da\u5206\u6790\u3001\u7db2\u9801\u958b\u767c\u9084\u662f\u81ea\u52d5\u5316\u8173\u672c\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002\u800c\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u8207\u6578\u64da\u5eab\u7684\u4ea4\u4e92\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528\u5eab\u5728Python\u4e2d\u5feb\u901f\u93c8\u63a5MySQL\u6578\u64da\u5eab\uff0c\u8b93\u4f60\u572830\u79d2\u5167\u638c\u63e1\u9019\u4e00\u6280\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afpymysql%ef%bc%9f-SFehERkvsI\">\u4ec0\u9ebc\u662fpymysql\uff1f<\/h2>\n<p>\u662f\u4e00\u500b\u7d14Python\u5be6\u73fe\u7684MySQL\u5ba2\u6236\u7aef\uff0c\u5141\u8a31Python\u7a0b\u5e8f\u8207MySQL\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u5b83\u652f\u6301Python 3.x\u7248\u672c\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e86\u7c21\u55ae\u6613\u7528\u7684API\uff0c\u4f7f\u5f97\u6578\u64da\u5eab\u64cd\u4f5c\u8b8a\u5f97\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9dpymysql-SFehERkvsI\">\u5b89\u88ddpymysql<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86\u5eab\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install pymysql<\/code><\/pre>\n<h2 id=\"%e9%8f%88%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%ad%a5%e9%a9%9f-SFehERkvsI\">\u93c8\u63a5\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f<\/h2>\n<p>\u93c8\u63a5MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5c0e\u5165\u5eab<\/li>\n<li>\u8a2d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\u53c3\u6578<\/li>\n<li>\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5<\/li>\n<li>\u57f7\u884cSQL\u8a9e\u53e5<\/li>\n<li>\u95dc\u9589\u9023\u63a5<\/li>\n<\/ol>\n<h2 id=\"%e7%a4%ba%e7%af%84%e4%bb%a3%e7%a2%bc-SFehERkvsI\">\u793a\u7bc4\u4ee3\u78bc<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u7bc4\u4ee3\u78bc\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u93c8\u63a5MySQL\u6578\u64da\u5eab\u4e26\u57f7\u884c\u4e00\u500b\u67e5\u8a62\uff1a<\/p>\n<pre><code>import pymysql\n\n# \u8a2d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\u53c3\u6578\nhost = 'localhost'  # \u6578\u64da\u5eab\u4e3b\u6a5f\nuser = 'your_username'  # \u6578\u64da\u5eab\u7528\u6236\u540d\npassword = 'your_password'  # \u6578\u64da\u5eab\u5bc6\u78bc\ndatabase = 'your_database'  # \u6578\u64da\u5eab\u540d\u7a31\n\n# \u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\nconnection = pymysql.connect(host=host,\n                             user=user,\n                             password=password,\n                             database=database)\n\ntry:\n    # \u5275\u5efa\u6e38\u6a19\u5c0d\u8c61\n    cursor = connection.cursor()\n    \n    # \u57f7\u884cSQL\u67e5\u8a62\n    sql = \"SELECT * FROM your_table\"\n    cursor.execute(sql)\n    \n    # \u7372\u53d6\u67e5\u8a62\u7d50\u679c\n    results = cursor.fetchall()\n    for row in results:\n        print(row)\nfinally:\n    # \u95dc\u9589\u6e38\u6a19\u548c\u9023\u63a5\n    cursor.close()\n    connection.close()\n<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-SFehERkvsI\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>\u78ba\u4fdd\u6578\u64da\u5eab\u670d\u52d9\u5668\u6b63\u5728\u904b\u884c\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a5\u53d7\u4f86\u81ea\u5ba2\u6236\u7aef\u7684\u9023\u63a5\u3002<\/li>\n<li>\u6aa2\u67e5\u7528\u6236\u540d\u548c\u5bc6\u78bc\u662f\u5426\u6b63\u78ba\uff0c\u4e26\u4e14\u8a72\u7528\u6236\u662f\u5426\u6709\u6b0a\u8a2a\u554f\u6307\u5b9a\u7684\u6578\u64da\u5eab\u3002<\/li>\n<li>\u5728\u57f7\u884cSQL\u8a9e\u53e5\u6642\uff0c\u6ce8\u610fSQL\u6ce8\u5165\u7684\u98a8\u96aa\uff0c\u5efa\u8b70\u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62\u4f86\u63d0\u9ad8\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-SFehERkvsI\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u4f7f\u7528\u5eab\u5728Python\u4e2d\u93c8\u63a5MySQL\u6578\u64da\u5eab\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff0c\u9084\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u3002\u7121\u8ad6\u4f60\u662f\u521d\u5b78\u8005\u9084\u662f\u6709\u7d93\u9a57\u7684\u958b\u767c\u8005\uff0c\u638c\u63e1\u9019\u4e00\u6280\u80fd\u90fd\u5c07\u5c0d\u4f60\u7684\u5de5\u4f5c\u5927\u6709\u88e8\u76ca\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u89e3\u6c7a\u65b9\u6848\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f\u6578\u64da\u5eab\u7ba1\u7406\u9084\u662f\u61c9\u7528\u90e8\u7f72\uff0c\u6211\u5011\u7684<a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\u90fd\u80fd\u70ba\u4f60\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528pymysql\u5feb\u901f\u9023\u63a5\u6578\u64da\u5eab\uff0c30\u79d2\u638c\u63e1Python\u7a0b\u5e8f\u5165\u9580\u5fc5\u5099\u6280\u5de7\uff0c\u8f15\u9b06\u7ba1\u7406\u6578\u64da\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-96998","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=96998"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96998\/revisions"}],"predecessor-version":[{"id":96999,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96998\/revisions\/96999"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}