{"id":97052,"date":"2024-10-20T14:36:17","date_gmt":"2024-10-20T06:36:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97052\/"},"modified":"2024-10-20T14:36:17","modified_gmt":"2024-10-20T06:36:17","slug":"python%e5%9c%a8%e8%bb%9f%e4%bb%b6%e9%96%8b%e7%99%bc%e4%b8%ad%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7-%e6%95%b8%e6%93%9a%e5%ba%ab%e6%87%89%e7%94%a8-python-%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97052\/","title":{"rendered":"Python\u5728\u8edf\u4ef6\u958b\u767c\u4e2d\u7684\u91cd\u8981\u6027\u2014\u2014\u6578\u64da\u5eab\u61c9\u7528 (python \u6578\u64da\u5eab \u8edf\u4ef6\u958b\u767c)"},"content":{"rendered":"<h1 id=\"python%e5%9c%a8%e8%bb%9f%e4%bb%b6%e9%96%8b%e7%99%bc%e4%b8%ad%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7-%e6%95%b8%e6%93%9a%e5%ba%ab%e6%87%89%e7%94%a8-UVfXAKRDuL\">Python\u5728\u8edf\u4ef6\u958b\u767c\u4e2d\u7684\u91cd\u8981\u6027\u2014\u2014\u6578\u64da\u5eab\u61c9\u7528<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u4ef6\u958b\u767c\u9818\u57df\uff0cPython\u5df2\u7d93\u6210\u70ba\u4e00\u7a2e\u4e0d\u53ef\u6216\u7f3a\u7684\u7de8\u7a0b\u8a9e\u8a00\u3002\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u5f37\u5927\u7684\u5eab\u652f\u6301\uff0c\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u69cb\u5efa\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\uff0c\u5c24\u5176\u662f\u5728\u6578\u64da\u5eab\u61c9\u7528\u65b9\u9762\u3002\u672c\u6587\u5c07\u63a2\u8a0ePython\u5728\u6578\u64da\u5eab\u61c9\u7528\u4e2d\u7684\u91cd\u8981\u6027\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"python%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%95%b4%e5%90%88-UVfXAKRDuL\">Python\u8207\u6578\u64da\u5eab\u7684\u6574\u5408<\/h2>\n<p>Python\u64c1\u6709\u591a\u7a2e\u5eab\u548c\u6846\u67b6\uff0c\u80fd\u5920\u8f15\u9b06\u5730\u8207\u5404\u7a2e\u6578\u64da\u5eab\u9032\u884c\u6574\u5408\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\u9078\u64c7\u6700\u9069\u5408\u5176\u9700\u6c42\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u3002<\/p>\n<h3 id=\"%e9%97%9c%e4%bf%82%e5%9e%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%87%89%e7%94%a8-UVfXAKRDuL\">\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7684\u61c9\u7528<\/h3>\n<p>\u5728\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u4e2d\uff0cPython\u7684<a href=\"https:\/\/www.sqlalchemy.org\/\" rel=\"nofollow noopener\" target=\"_blank\">SQLAlchemy<\/a>\u5eab\u662f\u4e00\u500b\u975e\u5e38\u6d41\u884c\u7684\u9078\u64c7\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u500b\u9ad8\u5c64\u6b21\u7684API\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u4ee5Python\u5c0d\u8c61\u7684\u65b9\u5f0f\u4f86\u64cd\u4f5c\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528SQLAlchemy\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>from sqlalchemy import create_engine\nfrom sqlalchemy.ext.declarative import declarative_base\nfrom sqlalchemy import Column, Integer, String\n\n# \u5275\u5efa\u6578\u64da\u5eab\u5f15\u64ce\nengine = create_engine('mysql+pymysql:\/\/username:password@localhost\/dbname')\n\n# \u5b9a\u7fa9\u57fa\u790e\u985e\nBase = declarative_base()\n\n# \u5b9a\u7fa9\u6578\u64da\u6a21\u578b\nclass User(Base):\n    __tablename__ = 'users'\n    id = Column(Integer, primary_key=True)\n    name = Column(String(50))\n\n# \u5275\u5efa\u6578\u64da\u8868\nBase.metadata.create_all(engine)\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5275\u5efa\u4e86\u4e00\u500b\u6578\u64da\u5eab\u5f15\u64ce\uff0c\u7136\u5f8c\u5b9a\u7fa9\u4e86\u4e00\u500b\u7528\u6236\u6a21\u578b\uff0c\u6700\u5f8c\u5275\u5efa\u4e86\u76f8\u61c9\u7684\u6578\u64da\u8868\u3002\u9019\u7a2e\u65b9\u5f0f\u4e0d\u50c5\u7c21\u5316\u4e86\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u9084\u63d0\u9ad8\u4e86\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u3002<\/p>\n<h3 id=\"%e9%9d%9e%e9%97%9c%e4%bf%82%e5%9e%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%87%89%e7%94%a8-UVfXAKRDuL\">\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7684\u61c9\u7528<\/h3>\n<p>\u5c0d\u65bc\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0cPython\u7684<a href=\"https:\/\/pymongo.readthedocs.io\/en\/stable\/\" rel=\"nofollow noopener\" target=\"_blank\">PyMongo<\/a>\u5eab\u662f\u8207MongoDB\u9032\u884c\u4ea4\u4e92\u7684\u7406\u60f3\u9078\u64c7\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528PyMongo\u9023\u63a5MongoDB\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>from pymongo import MongoClient\n\n# \u9023\u63a5\u5230MongoDB\nclient = MongoClient('localhost', 27017)\ndb = client['mydatabase']\n\n# \u63d2\u5165\u6578\u64da\nuser = {\"name\": \"Alice\", \"age\": 25}\ndb.users.insert_one(user)\n\n# \u67e5\u8a62\u6578\u64da\nfor user in db.users.find():\n    print(user)\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9023\u63a5\u5230\u672c\u5730\u7684MongoDB\u6578\u64da\u5eab\uff0c\u63d2\u5165\u4e86\u4e00\u689d\u7528\u6236\u6578\u64da\uff0c\u4e26\u67e5\u8a62\u6240\u6709\u7528\u6236\u3002\u9019\u7a2e\u9748\u6d3b\u6027\u4f7f\u5f97Python\u5728\u8655\u7406\u975e\u7d50\u69cb\u5316\u6578\u64da\u6642\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<h2 id=\"python%e7%9a%84%e5%84%aa%e5%8b%a2-UVfXAKRDuL\">Python\u7684\u512a\u52e2<\/h2>\n<p>Python\u5728\u6578\u64da\u5eab\u61c9\u7528\u4e2d\u7684\u512a\u52e2\u4e3b\u8981\u9ad4\u73fe\u5728\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u7c21\u55ae\u6613\u5b78\uff1a<\/strong>Python\u7684\u8a9e\u6cd5\u7c21\u6f54\uff0c\u9069\u5408\u521d\u5b78\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/li>\n<li><strong>\u5f37\u5927\u7684\u5eab\u652f\u6301\uff1a<\/strong>Python\u64c1\u6709\u773e\u591a\u7684\u7b2c\u4e09\u65b9\u5eab\uff0c\u80fd\u5920\u6eff\u8db3\u5404\u7a2e\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u9700\u6c42\u3002<\/li>\n<li><strong>\u826f\u597d\u7684\u793e\u5340\u652f\u6301\uff1a<\/strong>Python\u64c1\u6709\u6d3b\u8e8d\u7684\u958b\u767c\u8005\u793e\u5340\uff0c\u80fd\u5920\u63d0\u4f9b\u8c50\u5bcc\u7684\u8cc7\u6e90\u548c\u5e6b\u52a9\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-UVfXAKRDuL\">\u7d50\u8ad6<\/h2>\n<p>\u7e3d\u7684\u4f86\u8aaa\uff0cPython\u5728\u8edf\u4ef6\u958b\u767c\u4e2d\u7684\u91cd\u8981\u6027\u4e0d\u5bb9\u5ffd\u8996\uff0c\u5c24\u5176\u662f\u5728\u6578\u64da\u5eab\u61c9\u7528\u65b9\u9762\u3002\u7121\u8ad6\u662f\u95dc\u4fc2\u578b\u9084\u662f\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u9748\u6d3b\u4e14\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u96a8\u8457\u6578\u64da\u9a45\u52d5\u61c9\u7528\u7684\u589e\u9577\uff0c\u638c\u63e1Python\u53ca\u5176\u6578\u64da\u5eab\u61c9\u7528\u5c07\u6210\u70ba\u958b\u767c\u8005\u7684\u4e00\u9805\u91cd\u8981\u6280\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728\u60a8\u7684\u9805\u76ee\u4e2d\u4f7f\u7528Python\u9032\u884c\u6578\u64da\u5eab\u958b\u767c\u6709\u8208\u8da3\uff0c\u6216\u8005\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c<a href=\"https:\/\/server.hk\">\u96f2\u670d\u52d9\u5668<\/a>\u7684\u8cc7\u8a0a\uff0c\u8acb\u96a8\u6642\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Python\u5728\u8edf\u4ef6\u958b\u767c\u4e2d\u7684\u91cd\u8981\u6027\uff0c\u7279\u5225\u662f\u5728\u6578\u64da\u5eab\u61c9\u7528\u65b9\u9762\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u8207\u6578\u64da\u8655\u7406\u80fd\u529b\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-97052","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97052","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=97052"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97052\/revisions"}],"predecessor-version":[{"id":97053,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97052\/revisions\/97053"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}