{"id":97036,"date":"2024-10-20T14:28:17","date_gmt":"2024-10-20T06:28:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97036\/"},"modified":"2024-10-20T14:28:18","modified_gmt":"2024-10-20T06:28:18","slug":"python%e4%bd%bf%e7%94%a8pymysql%e7%99%bb%e9%8c%84%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%96%b9%e6%b3%95-pymysql%e7%99%bb%e9%8c%84%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97036\/","title":{"rendered":"Python\u4f7f\u7528pymysql\u767b\u9304\u6578\u64da\u5eab\u7684\u65b9\u6cd5 (pymysql\u767b\u9304\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python%e4%bd%bf%e7%94%a8pymysql%e7%99%bb%e9%8c%84%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%96%b9%e6%b3%95-lWkfrMGPvr\">Python\u4f7f\u7528pymysql\u767b\u9304\u6578\u64da\u5eab\u7684\u65b9\u6cd5<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u666e\u904d\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u4e14\u6613\u65bc\u5b78\u7fd2\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528pymysql\u5eab\u4f86\u767b\u9304MySQL\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afpymysql%ef%bc%9f-lWkfrMGPvr\">\u4ec0\u9ebc\u662fpymysql\uff1f<\/h2>\n<p>pymysql\u662f\u4e00\u500b\u7d14Python\u7de8\u5beb\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\u662fMySQLdb\u7684\u66ff\u4ee3\u54c1\u3002pymysql\u7684\u512a\u52e2\u5728\u65bc\u5176\u7c21\u55ae\u6613\u7528\uff0c\u4e26\u4e14\u80fd\u5920\u8f15\u9b06\u5730\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9dpymysql-lWkfrMGPvr\">\u5b89\u88ddpymysql<\/h2>\n<p>\u5728\u4f7f\u7528pymysql\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd\u8a72\u5eab\u3002\u53ef\u4ee5\u901a\u904epip\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install pymysql<\/code><\/pre>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e5%88%b0mysql%e6%95%b8%e6%93%9a%e5%ba%ab-lWkfrMGPvr\">\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u63a5\u4e0b\u4f86\u5c31\u662f\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u9023\u63a5\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u5c0e\u5165pymysql\u5eab<\/li>\n<li>\u4f7f\u7528pymysql.connect()\u65b9\u6cd5\u5efa\u7acb\u9023\u63a5<\/li>\n<li>\u5275\u5efa\u6e38\u6a19\u5c0d\u8c61\u4ee5\u57f7\u884cSQL\u67e5\u8a62<\/li>\n<li>\u95dc\u9589\u9023\u63a5<\/li>\n<\/ol>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-lWkfrMGPvr\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528pymysql\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>import pymysql\n\n# \u9023\u63a5\u5230\u6578\u64da\u5eab\nconnection = pymysql.connect(\n    host='localhost',      # \u6578\u64da\u5eab\u4e3b\u6a5f\u5730\u5740\n    user='your_username',  # \u6578\u64da\u5eab\u7528\u6236\u540d\n    password='your_password',  # \u6578\u64da\u5eab\u5bc6\u78bc\n    database='your_database'   # \u6578\u64da\u5eab\u540d\u7a31\n)\n\ntry:\n    # \u5275\u5efa\u6e38\u6a19\u5c0d\u8c61\n    with connection.cursor() as cursor:\n        # \u57f7\u884cSQL\u67e5\u8a62\n        sql = \"SELECT * FROM your_table\"\n        cursor.execute(sql)\n        \n        # \u7372\u53d6\u67e5\u8a62\u7d50\u679c\n        result = cursor.fetchall()\n        for row in result:\n            print(row)\nfinally:\n    # \u95dc\u9589\u9023\u63a5\n    connection.close()\n<\/code><\/pre>\n<h2 id=\"%e8%99%95%e7%90%86%e7%95%b0%e5%b8%b8-lWkfrMGPvr\">\u8655\u7406\u7570\u5e38<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u8655\u7406\u7570\u5e38\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002pymysql\u63d0\u4f9b\u4e86\u591a\u7a2e\u7570\u5e38\u985e\u578b\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u6355\u7372\u548c\u8655\u7406\u932f\u8aa4\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u8655\u7406\u7570\u5e38\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>try:\n    connection = pymysql.connect(\n        host='localhost',\n        user='your_username',\n        password='your_password',\n        database='your_database'\n    )\nexcept pymysql.MySQLError as e:\n    print(f\"Error connecting to database: {e}\")\nfinally:\n    if connection:\n        connection.close()\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-lWkfrMGPvr\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528pymysql\u9023\u63a5MySQL\u6578\u64da\u5eab\u662f\u4e00\u500b\u7c21\u55ae\u800c\u6709\u6548\u7684\u904e\u7a0b\u3002\u901a\u904e\u5b89\u88ddpymysql\u5eab\u3001\u5efa\u7acb\u9023\u63a5\u3001\u57f7\u884c\u67e5\u8a62\u4ee5\u53ca\u8655\u7406\u7570\u5e38\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u7121\u8ad6\u662f\u958b\u767c\u5c0f\u578b\u61c9\u7528\u9084\u662f\u5927\u578b\u7cfb\u7d71\uff0cpymysql\u90fd\u80fd\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684Python\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528pymysql\u5eab\u5728Python\u4e2d\u767b\u9304\u6578\u64da\u5eab\uff0c\u7c21\u55ae\u6b65\u9a5f\u548c\u793a\u4f8b\u4ee3\u78bc\u52a9\u4f60\u8f15\u9b06\u9023\u63a5MySQL\u6578\u64da\u5eab\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-97036","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97036","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=97036"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97036\/revisions"}],"predecessor-version":[{"id":97037,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97036\/revisions\/97037"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}