{"id":97034,"date":"2024-10-20T14:27:17","date_gmt":"2024-10-20T06:27:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97034\/"},"modified":"2024-10-20T14:27:18","modified_gmt":"2024-10-20T06:27:18","slug":"python%e5%af%a6%e7%8f%be%e9%86%ab%e9%99%a2%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8e%a5%e5%85%a5-python-%e6%8e%a5%e5%85%a5%e9%86%ab%e9%99%a2%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97034\/","title":{"rendered":"Python\u5be6\u73fe\u91ab\u9662\u6578\u64da\u5eab\u63a5\u5165 (python \u63a5\u5165\u91ab\u9662\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python%e5%af%a6%e7%8f%be%e9%86%ab%e9%99%a2%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8e%a5%e5%85%a5-AeKkEmyUuh\">Python\u5be6\u73fe\u91ab\u9662\u6578\u64da\u5eab\u63a5\u5165<\/h1>\n<p>\u96a8\u8457\u91ab\u7642\u884c\u696d\u7684\u6578\u5b57\u5316\u8f49\u578b\uff0c\u91ab\u9662\u6578\u64da\u5eab\u7684\u63a5\u5165\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u4e14\u6613\u65bc\u5b78\u7fd2\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5df2\u6210\u70ba\u8a31\u591a\u958b\u767c\u8005\u5728\u91ab\u7642\u6578\u64da\u7ba1\u7406\u4e2d\u9996\u9078\u7684\u5de5\u5177\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Python\u63a5\u5165\u91ab\u9662\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e9%86%ab%e9%99%a2%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-AeKkEmyUuh\">\u91ab\u9662\u6578\u64da\u5eab\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u91ab\u9662\u6578\u64da\u5eab\u901a\u5e38\u5305\u542b\u75c5\u4eba\u4fe1\u606f\u3001\u91ab\u7642\u8a18\u9304\u3001\u85e5\u7269\u8655\u65b9\u3001\u6aa2\u67e5\u7d50\u679c\u7b49\u591a\u7a2e\u6578\u64da\u3002\u9019\u4e9b\u6578\u64da\u7684\u7ba1\u7406\u548c\u5206\u6790\u5c0d\u65bc\u63d0\u9ad8\u91ab\u7642\u670d\u52d9\u8cea\u91cf\u81f3\u95dc\u91cd\u8981\u3002\u6578\u64da\u5eab\u7684\u63a5\u5165\u53ef\u4ee5\u5e6b\u52a9\u91ab\u7642\u5de5\u4f5c\u8005\u5feb\u901f\u7372\u53d6\u6240\u9700\u4fe1\u606f\uff0c\u5f9e\u800c\u505a\u51fa\u66f4\u597d\u7684\u6c7a\u7b56\u3002<\/p>\n<h2 id=\"%e9%81%b8%e6%93%87%e5%90%88%e9%81%a9%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab-AeKkEmyUuh\">\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u958b\u59cb\u63a5\u5165\u91ab\u9662\u6578\u64da\u5eab\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u3002\u5e38\u898b\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>MySQL<\/li>\n<li>PostgreSQL<\/li>\n<li>SQLite<\/li>\n<li>MongoDB<\/li>\n<\/ul>\n<p>\u6839\u64da\u91ab\u9662\u7684\u9700\u6c42\u548c\u6578\u64da\u91cf\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8python%e6%8e%a5%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-AeKkEmyUuh\">\u4f7f\u7528Python\u63a5\u5165\u6578\u64da\u5eab<\/h2>\n<p>Python\u63d0\u4f9b\u4e86\u591a\u7a2e\u5eab\u4f86\u5be6\u73fe\u6578\u64da\u5eab\u7684\u63a5\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u5eab\uff1a<\/p>\n<ul>\n<li><code>mysql-connector-python<\/code>\uff1a\u7528\u65bcMySQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002<\/li>\n<li><code>psycopg2<\/code>\uff1a\u7528\u65bcPostgreSQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002<\/li>\n<li><code>sqlite3<\/code>\uff1a\u7528\u65bcSQLite\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002<\/li>\n<li><code>pymongo<\/code>\uff1a\u7528\u65bcMongoDB\u7684\u9023\u63a5\u3002<\/li>\n<\/ul>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8mysql%e6%95%b8%e6%93%9a%e5%ba%ab-AeKkEmyUuh\">\u793a\u4f8b\uff1a\u4f7f\u7528MySQL\u6578\u64da\u5eab<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528<code>mysql-connector-python<\/code>\u5eab\u63a5\u5165MySQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import mysql.connector\n\n# \u9023\u63a5\u5230\u6578\u64da\u5eab\ndb = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"yourusername\",\n    password=\"yourpassword\",\n    database=\"hospital_db\"\n)\n\n# \u5275\u5efa\u6e38\u6a19\ncursor = db.cursor()\n\n# \u57f7\u884c\u67e5\u8a62\ncursor.execute(\"SELECT * FROM patients\")\n\n# \u7372\u53d6\u7d50\u679c\nresults = cursor.fetchall()\n\nfor row in results:\n    print(row)\n\n# \u95dc\u9589\u9023\u63a5\ncursor.close()\ndb.close()\n<\/code><\/pre>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8postgresql%e6%95%b8%e6%93%9a%e5%ba%ab-AeKkEmyUuh\">\u793a\u4f8b\uff1a\u4f7f\u7528PostgreSQL\u6578\u64da\u5eab<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528<code>psycopg2<\/code>\u5eab\u63a5\u5165PostgreSQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import psycopg2\n\n# \u9023\u63a5\u5230\u6578\u64da\u5eab\nconn = psycopg2.connect(\n    dbname=\"hospital_db\",\n    user=\"yourusername\",\n    password=\"yourpassword\",\n    host=\"localhost\"\n)\n\n# \u5275\u5efa\u6e38\u6a19\ncur = conn.cursor()\n\n# \u57f7\u884c\u67e5\u8a62\ncur.execute(\"SELECT * FROM patients\")\n\n# \u7372\u53d6\u7d50\u679c\nrows = cur.fetchall()\n\nfor row in rows:\n    print(row)\n\n# \u95dc\u9589\u9023\u63a5\ncur.close()\nconn.close()\n<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-AeKkEmyUuh\">\u6578\u64da\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u91ab\u7642\u884c\u696d\uff0c\u6578\u64da\u7684\u5b89\u5168\u6027\u81f3\u95dc\u91cd\u8981\u3002\u63a5\u5165\u91ab\u9662\u6578\u64da\u5eab\u6642\uff0c\u61c9\u8003\u616e\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u52a0\u5bc6\u6280\u8853\u4fdd\u8b77\u6578\u64da\u50b3\u8f38\u3002<\/li>\n<li>\u8a2d\u7f6e\u9069\u7576\u7684\u7528\u6236\u6b0a\u9650\uff0c\u9650\u5236\u6578\u64da\u8a2a\u554f\u3002<\/li>\n<li>\u5b9a\u671f\u5099\u4efd\u6578\u64da\uff0c\u4ee5\u9632\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-AeKkEmyUuh\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Python\u63a5\u5165\u91ab\u9662\u6578\u64da\u5eab\u4e0d\u50c5\u80fd\u63d0\u9ad8\u6578\u64da\u7ba1\u7406\u7684\u6548\u7387\uff0c\u9084\u80fd\u70ba\u91ab\u7642\u5de5\u4f5c\u8005\u63d0\u4f9b\u66f4\u597d\u7684\u652f\u6301\u3002\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u548c\u5b89\u5168\u63aa\u65bd\u662f\u6210\u529f\u63a5\u5165\u7684\u95dc\u9375\u3002\u5c0d\u65bc\u9700\u8981\u7a69\u5b9a\u548c\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u7684\u91ab\u7642\u6a5f\u69cb\uff0c\u9078\u64c7\u5408\u9069\u7684<code><a href=\"https:\/\/server.hk\">VPS<\/a><\/code>\u89e3\u6c7a\u65b9\u6848\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u6574\u9ad4\u904b\u71df\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Python\u63a5\u5165\u91ab\u9662\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u6578\u64da\u7ba1\u7406\u8207\u5206\u6790\uff0c\u63d0\u5347\u91ab\u7642\u670d\u52d9\u6548\u7387\u8207\u6e96\u78ba\u6027\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-97034","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97034","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=97034"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97034\/revisions"}],"predecessor-version":[{"id":97035,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97034\/revisions\/97035"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}