{"id":205178,"date":"2025-05-29T15:18:03","date_gmt":"2025-05-29T07:18:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205178\/"},"modified":"2025-05-29T15:18:03","modified_gmt":"2025-05-29T07:18:03","slug":"python-%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c%ef%bc%9a%e5%bf%85%e9%a1%bb%e6%98%a0%e5%b0%84%e5%ad%97%e6%ae%b5%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205178\/","title":{"rendered":"Python \u6570\u636e\u5e93\u64cd\u4f5c\uff1a\u5fc5\u987b\u6620\u5c04\u5b57\u6bb5\u5417\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u6570\u636e\u5e93\u64cd\u4f5c\uff1a\u5fc5\u987b\u6620\u5c04\u5b57\u6bb5\u5417\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aPython \u6570\u636e\u5e93\u64cd\u4f5c\uff1a\u5fc5\u987b\u6620\u5c04\u5b57\u6bb5\u5417\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241125\/1732520377674429b93b951.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u6570\u636e\u5e93\u64cd\u4f5c\u662f\u5426\u5fc5\u987b\u6620\u5c04\u5b57\u6bb5\uff1f<\/strong><\/p>\n<p>\u5728 python \u4e2d\uff0c\u4f7f\u7528 sqlalchemy \u548c mongodb \u7b49\u6846\u67b6\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5b57\u6bb5\u6620\u5c04\u7684\u95ee\u9898\u3002\u90a3\u4e48\uff0cpython \u7684\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u5426\u5fc5\u987b\u6620\u5c04\u5b57\u6bb5\u5462\uff1f<\/p>\n<p>\u7b54\u6848\u662f\u5426\u5b9a\u7684\u3002\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5728\u4e0d\u6620\u5c04\u5b57\u6bb5\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff1a<\/p>\n<p><strong>\u539f\u59cb sql \u67e5\u8be2<\/strong><\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b98\u65b9\u7684 mysql \u8fde\u63a5\u5668 pymysql \u76f4\u63a5\u6267\u884c\u539f\u59cb sql \u67e5\u8be2\uff0c\u800c\u65e0\u9700\u5b9a\u4e49\u6620\u5c04\u7c7b\u3002<\/p>\n<pre>import pymysql\n\nconnection = pymysql.connect(host='localhost',\n                             user='username',\n                             password='password',\n                             db='mydb',\n                             charset='utf8mb4',\n                             cursorclass=pymysql.cursors.dictcursor)\n\ntry:\n    with connection.cursor() as cursor:\n        sql = \"select * from mytable\"\n        cursor.execute(sql)\n        result = cursor.fetchall()\n        print(result)\nfinally:\n    connection.close()<\/pre>\n<p><strong>mongodb \u539f\u751f\u9a71\u52a8<\/strong><\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b98\u65b9\u7684 pymongo \u9a71\u52a8\u7a0b\u5e8f\u76f4\u63a5\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u6ca1\u5fc5\u8981\u4e3a\u6bcf\u4e2a\u8868\u5b9a\u4e49\u6620\u5c04\u7c7b\u3002<\/p>\n<pre>import pymongo\n\nclient = pymongo.MongoClient(\"mongodb:\/\/localhost:27017\/\")\ndb = client[\"mydatabase\"]\ncollection = db[\"mycollection\"]\n\n# \u67e5\u8be2\u6570\u636e\nresults = collection.find({})\n\nfor result in results:\n    print(result)<\/pre>\n<p>\u4ee5\u4e0a\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e0d\u6620\u5c04\u5b57\u6bb5\u7684\u60c5\u51b5\u4e0b\u76f4\u63a5\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u64cd\u4f5c\uff0c\u907f\u514d\u4e86\u521b\u5efa\u5927\u91cf\u5bf9\u8c61\u6587\u4ef6\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPython \u6570\u636e\u5e93\u64cd\u4f5c\uff1a\u5fc5\u987b\u6620\u5c04\u5b57\u6bb5\u5417\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u6570\u636e\u5e93\u64cd\u4f5c\uff1a\u5fc5\u987b\u6620&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205178","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205178","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205178"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205178\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}