{"id":97050,"date":"2024-10-20T14:35:17","date_gmt":"2024-10-20T06:35:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97050\/"},"modified":"2024-10-20T14:35:17","modified_gmt":"2024-10-20T06:35:17","slug":"python%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%a4%ba%e4%be%8b-python%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%abdemo","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97050\/","title":{"rendered":"Python\u8207\u6578\u64da\u5eab\u9023\u63a5\u793a\u4f8b (python\u9023\u63a5\u6578\u64da\u5eabdemo)"},"content":{"rendered":"<h1 id=\"python%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%a4%ba%e4%be%8b-python%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%abdemo-FTzHfQGOjP\">Python\u8207\u6578\u64da\u5eab\u9023\u63a5\u793a\u4f8b (python\u9023\u63a5\u6578\u64da\u5eabdemo)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0cPython\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u4e14\u6613\u65bc\u5b78\u7fd2\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5df2\u7d93\u6210\u70ba\u6578\u64da\u79d1\u5b78\u3001\u7db2\u9801\u958b\u767c\u548c\u81ea\u52d5\u5316\u7b49\u9818\u57df\u7684\u71b1\u9580\u9078\u64c7\u3002\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u662fPython\u61c9\u7528\u7a0b\u5e8f\u4e2d\u4e00\u500b\u91cd\u8981\u7684\u7d44\u6210\u90e8\u5206\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Python\u9023\u63a5\u4e0d\u540c\u985e\u578b\u7684\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87python%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-FTzHfQGOjP\">\u70ba\u4ec0\u9ebc\u9078\u64c7Python\u9023\u63a5\u6578\u64da\u5eab\uff1f<\/h2>\n<p>Python\u64c1\u6709\u591a\u7a2e\u5eab\u548c\u6846\u67b6\uff0c\u4f7f\u5f97\u8207\u6578\u64da\u5eab\u7684\u4ea4\u4e92\u8b8a\u5f97\u7c21\u55ae\u800c\u9ad8\u6548\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\u63d0\u4f9b\u826f\u597d\u7684\u652f\u6301\u3002\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3001\u67e5\u8a62\u548c\u7ba1\u7406\u3002<\/p>\n<h2 id=\"%e9%80%a3%e6%8e%a5mysql%e6%95%b8%e6%93%9a%e5%ba%ab-FTzHfQGOjP\">\u9023\u63a5MySQL\u6578\u64da\u5eab<\/h2>\n<p>MySQL\u662f\u4e00\u7a2e\u6d41\u884c\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0cPython\u53ef\u4ee5\u901a\u904e`mysql-connector-python`\u5eab\u4f86\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import mysql.connector\n\n# \u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\nconn = 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 = conn.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()\nconn.close()\n<\/code><\/pre>\n<h2 id=\"%e9%80%a3%e6%8e%a5postgresql%e6%95%b8%e6%93%9a%e5%ba%ab-FTzHfQGOjP\">\u9023\u63a5PostgreSQL\u6578\u64da\u5eab<\/h2>\n<p>PostgreSQL\u662f\u4e00\u7a2e\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0cPython\u53ef\u4ee5\u901a\u904e`psycopg2`\u5eab\u4f86\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u9023\u63a5PostgreSQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import psycopg2\n\n# \u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\nconn = 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 = conn.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()\nconn.close()\n<\/code><\/pre>\n<h2 id=\"%e9%80%a3%e6%8e%a5mongodb%e6%95%b8%e6%93%9a%e5%ba%ab-FTzHfQGOjP\">\u9023\u63a5MongoDB\u6578\u64da\u5eab<\/h2>\n<p>MongoDB\u662f\u4e00\u7a2e\u6d41\u884c\u7684\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0cPython\u53ef\u4ee5\u901a\u904e`pymongo`\u5eab\u4f86\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u9023\u63a5MongoDB\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>from pymongo import MongoClient\n\n# \u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\nclient = MongoClient(\"mongodb:\/\/localhost:27017\/\")\ndb = client[\"your_database\"]\n\n# \u7372\u53d6\u96c6\u5408\ncollection = db[\"your_collection\"]\n\n# \u67e5\u8a62\u6578\u64da\nresults = collection.find()\nfor document in results:\n    print(document)\n\n# \u95dc\u9589\u9023\u63a5\nclient.close()\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-FTzHfQGOjP\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528Python\u9023\u63a5\u4e0d\u540c\u985e\u578b\u7684\u6578\u64da\u5eab\uff0c\u5305\u62ecMySQL\u3001PostgreSQL\u548cMongoDB\u3002\u9019\u4e9b\u793a\u4f8b\u4ee3\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u5efa\u7acb\u9023\u63a5\u3001\u57f7\u884c\u67e5\u8a62\u4ee5\u53ca\u7372\u53d6\u7d50\u679c\u3002\u96a8\u8457\u6578\u64da\u9700\u6c42\u7684\u589e\u9577\uff0c\u638c\u63e1Python\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u5c07\u6210\u70ba\u958b\u767c\u8005\u7684\u91cd\u8981\u6280\u80fd\u3002<\/p>\n<p>\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\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u5730\u5340\u7684\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u9ad8\u6548\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Python\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u793a\u4f8b\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528\u7c21\u55ae\u7684\u4ee3\u78bc\u5be6\u73fe\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u63d0\u5347\u4f60\u7684\u958b\u767c\u6280\u80fd\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-97050","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97050","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=97050"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97050\/revisions"}],"predecessor-version":[{"id":97051,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97050\/revisions\/97051"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}