{"id":97030,"date":"2024-10-20T14:25:18","date_gmt":"2024-10-20T06:25:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97030\/"},"modified":"2024-10-20T14:25:19","modified_gmt":"2024-10-20T06:25:19","slug":"python%e5%af%a6%e7%8f%be%e5%bf%ab%e9%80%9f%e9%ab%98%e6%95%88%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-python%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97030\/","title":{"rendered":"Python\u5be6\u73fe\u5feb\u901f\u9ad8\u6548\u7684\u6578\u64da\u5eab\u9023\u63a5 (python\u9023\u63a5\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python%e5%af%a6%e7%8f%be%e5%bf%ab%e9%80%9f%e9%ab%98%e6%95%88%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-NgiVrgwEln\">Python\u5be6\u73fe\u5feb\u901f\u9ad8\u6548\u7684\u6578\u64da\u5eab\u9023\u63a5<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u9ad8\u6548\u9023\u63a5\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5eab\u548c\u5de5\u5177\u4f86\u5be6\u73fe\u8207\u5404\u7a2e\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Python\u5be6\u73fe\u5feb\u901f\u9ad8\u6548\u7684\u6578\u64da\u5eab\u9023\u63a5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87python%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-NgiVrgwEln\">\u70ba\u4ec0\u9ebc\u9078\u64c7Python\u9032\u884c\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>Python\u56e0\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u5f37\u5927\u7684\u5eab\u652f\u6301\u800c\u53d7\u5230\u5ee3\u6cdb\u6b61\u8fce\u3002\u7121\u8ad6\u662f\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982MySQL\u3001PostgreSQL\uff09\u9084\u662f\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982MongoDB\uff09\uff0cPython\u90fd\u80fd\u8f15\u9b06\u61c9\u5c0d\u3002\u6b64\u5916\uff0cPython\u7684\u7570\u6b65\u7de8\u7a0b\u7279\u6027\u4f7f\u5f97\u5728\u9ad8\u4e26\u767c\u74b0\u5883\u4e0b\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e7%94%a8%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e5%ba%ab-NgiVrgwEln\">\u5e38\u7528\u7684\u6578\u64da\u5eab\u9023\u63a5\u5eab<\/h2>\n<ul>\n<li><strong>MySQL Connector\/Python<\/strong>: \u7528\u65bc\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u5b98\u65b9\u5eab\u3002<\/li>\n<li><strong>psycopg2<\/strong>: \u7528\u65bc\u9023\u63a5PostgreSQL\u6578\u64da\u5eab\u7684\u6d41\u884c\u5eab\u3002<\/li>\n<li><strong>SQLAlchemy<\/strong>: \u4e00\u500b\u5f37\u5927\u7684ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u5eab\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u3002<\/li>\n<li><strong>pymongo<\/strong>: \u7528\u65bc\u9023\u63a5MongoDB\u7684\u5b98\u65b9\u5eab\u3002<\/li>\n<\/ul>\n<h2 id=\"%e4%bd%bf%e7%94%a8mysql-connector-python%e9%80%a3%e6%8e%a5mysql%e6%95%b8%e6%93%9a%e5%ba%ab-NgiVrgwEln\">\u4f7f\u7528MySQL Connector\/Python\u9023\u63a5MySQL\u6578\u64da\u5eab<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528MySQL Connector\/Python\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<pre><code>import mysql.connector\n\n# \u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\nconnection = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"your_username\",\n    password=\"your_password\",\n    database=\"your_database\"\n)\n\n# \u5275\u5efa\u6e38\u6a19\u5c0d\u8c61\ncursor = connection.cursor()\n\n# \u57f7\u884c\u67e5\u8a62\ncursor.execute(\"SELECT * FROM your_table\")\n\n# \u7372\u53d6\u7d50\u679c\nresults = cursor.fetchall()\nfor row in results:\n    print(row)\n\n# \u95dc\u9589\u9023\u63a5\ncursor.close()\nconnection.close()\n<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8psycopg2%e9%80%a3%e6%8e%a5postgresql%e6%95%b8%e6%93%9a%e5%ba%ab-NgiVrgwEln\">\u4f7f\u7528psycopg2\u9023\u63a5PostgreSQL\u6578\u64da\u5eab<\/h2>\n<p>\u5c0d\u65bcPostgreSQL\u6578\u64da\u5eab\uff0cpsycopg2\u662f\u6700\u5e38\u7528\u7684\u5eab\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u9023\u63a5\u793a\u4f8b\uff1a<\/p>\n<pre><code>import psycopg2\n\n# \u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\nconnection = psycopg2.connect(\n    host=\"localhost\",\n    database=\"your_database\",\n    user=\"your_username\",\n    password=\"your_password\"\n)\n\n# \u5275\u5efa\u6e38\u6a19\u5c0d\u8c61\ncursor = connection.cursor()\n\n# \u57f7\u884c\u67e5\u8a62\ncursor.execute(\"SELECT * FROM your_table\")\n\n# \u7372\u53d6\u7d50\u679c\nresults = cursor.fetchall()\nfor row in results:\n    print(row)\n\n# \u95dc\u9589\u9023\u63a5\ncursor.close()\nconnection.close()\n<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-NgiVrgwEln\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u4f7f\u7528\u9023\u63a5\u6c60<\/strong>: \u5728\u9ad8\u4e26\u767c\u74b0\u5883\u4e2d\uff0c\u4f7f\u7528\u9023\u63a5\u6c60\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528\u5982SQLAlchemy\u7684\u9023\u63a5\u6c60\u529f\u80fd\u3002<\/li>\n<li><strong>\u7570\u6b65\u64cd\u4f5c<\/strong>: \u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u8655\u7406\u5927\u91cf\u8acb\u6c42\u7684\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528Python\u7684\u7570\u6b65\u5eab\uff08\u5982asyncio\uff09\u4f86\u5be6\u73fe\u975e\u963b\u585e\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u932f\u8aa4\u8655\u7406<\/strong>: \u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u52d9\u5fc5\u6dfb\u52a0\u932f\u8aa4\u8655\u7406\u6a5f\u5236\uff0c\u4ee5\u78ba\u4fdd\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-NgiVrgwEln\">\u7e3d\u7d50<\/h2>\n<p>Python\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u5f0f\u4f86\u5be6\u73fe\u5feb\u901f\u9ad8\u6548\u7684\u6578\u64da\u5eab\u9023\u63a5\uff0c\u7121\u8ad6\u662f\u4f7f\u7528\u539f\u751f\u5eab\u9084\u662fORM\u5de5\u5177\u3002\u901a\u904e\u9075\u5faa\u6700\u4f73\u5be6\u8e10\uff0c\u5982\u4f7f\u7528\u9023\u63a5\u6c60\u548c\u7570\u6b65\u64cd\u4f5c\uff0c\u53ef\u4ee5\u9032\u4e00\u6b65\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684Python\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\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\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Python\u5be6\u73fe\u5feb\u901f\u9ad8\u6548\u7684\u6578\u64da\u5eab\u9023\u63a5\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6027\u80fd\uff0c\u7c21\u5316\u958b\u767c\u6d41\u7a0b\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-97030","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97030","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=97030"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97030\/revisions"}],"predecessor-version":[{"id":97031,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97030\/revisions\/97031"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}