{"id":205069,"date":"2025-05-29T09:02:10","date_gmt":"2025-05-29T01:02:10","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205069\/"},"modified":"2025-05-29T09:02:10","modified_gmt":"2025-05-29T01:02:10","slug":"python-%e6%95%b0%e6%8d%ae%e6%93%8d%e4%bd%9c%e7%9c%9f%e7%9a%84%e9%9c%80%e8%a6%81%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\/205069\/","title":{"rendered":"Python \u6570\u636e\u64cd\u4f5c\u771f\u7684\u9700\u8981\u6620\u5c04\u5b57\u6bb5\u5417\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u6570\u636e\u64cd\u4f5c\u771f\u7684\u9700\u8981\u6620\u5c04\u5b57\u6bb5\u5417\uff1f<\/h1>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300aPython \u6570\u636e\u64cd\u4f5c\u771f\u7684\u9700\u8981\u6620\u5c04\u5b57\u6bb5\u5417\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241122\/173228008867407f18f337c.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u6570\u636e\u64cd\u4f5c\u662f\u5426\u9700\u8981\u6620\u5c04\u5b57\u6bb5\uff1f<\/strong><\/p>\n<p>\u5728 python \u4e2d\u4f7f\u7528 sqlalchemy \u6216 mongodb \u65f6\uff0c\u4f60\u53ef\u80fd\u9047\u5230\u9700\u8981\u6620\u5c04\u5b57\u6bb5\u7684\u95ee\u9898\u3002\u90a3\u4e48\uff0c\u8fd9\u79cd\u6620\u5c04\u5bf9\u4e8e\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u5426\u5fc5\u9700\u7684\u5462\uff1f<\/p>\n<p><strong>\u7b54\u6848\uff1a\u4e0d\u5fc5\u9700<\/strong><\/p>\n<p>python \u4e2d\u53ef\u4ee5\u4f7f\u7528\u539f\u751f\u6570\u636e\u5e93\u64cd\u4f5c\u5e93\u8fdb\u884c\u76f4\u63a5\u7684\u6570\u636e\u67e5\u8be2\uff0c\u5982\uff1a<\/p>\n<p><strong>sqlalchemy \u76f4\u63a5\u67e5\u8be2<\/strong><\/p>\n<p>\u4f7f\u7528 pymysql \u5e93\uff1a<\/p>\n<pre>import pymysql\n\nconnection = pymysql.connect(...)\n\nwith connection.cursor() as cursor:\n    sql = \"select * from mytable\"\n    cursor.execute(sql)\n    result = cursor.fetchall()\n    print(result)<\/pre>\n<p><strong>mongodb \u76f4\u63a5\u67e5\u8be2<\/strong><\/p>\n<p>\u4f7f\u7528 pymongo \u5e93\uff1a<\/p>\n<pre>import pymongo\n\nclient = pymongo.MongoClient(...)\ncollection = client[\"mydatabase\"][\"mycollection\"]\n\nresults = collection.find({})\n\nfor result in results:\n    print(result)<\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u76f4\u63a5\u6267\u884c sql \u67e5\u8be2\u6216 mongodb \u67e5\u627e\uff0c\u800c\u65e0\u9700\u6620\u5c04\u5bf9\u8c61\u5230\u6570\u636e\u5e93\u8868\u3002\u8fd9\u5bf9\u4e8e\u64cd\u4f5c\u591a\u4e2a\u8868\u6216\u907f\u514d\u521b\u5efa\u5927\u91cf\u5bf9\u8c61\u6587\u4ef6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPython \u6570\u636e\u64cd\u4f5c\u771f\u7684\u9700\u8981\u6620\u5c04\u5b57\u6bb5\u5417\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u6570\u636e\u64cd\u4f5c\u771f\u7684\u9700\u8981\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-205069","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205069","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=205069"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205069\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}