{"id":582,"date":"2024-05-08T19:02:33","date_gmt":"2024-05-08T11:02:33","guid":{"rendered":"http:\/\/www.madbull.site\/?p=582"},"modified":"2024-05-08T19:05:06","modified_gmt":"2024-05-08T11:05:06","slug":"sqlalchemy-orm-%e6%96%b9%e5%bc%8f%e5%81%9amysql%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=582","title":{"rendered":"sqlalchemy orm \u65b9\u5f0f\u505amysql\u5904\u7406"},"content":{"rendered":"\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u521b\u5efa\u6570\u636e\u5e93learn\uff0c\u5e76\u5728\u5e93\u4e2d\u521b\u5efa\u8868ntbl<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE `ntbl` (\n  `id` int NOT NULL AUTO_INCREMENT,\n  `name` varchar(45) COLLATE utf8_bin NOT NULL,\n  `age` int NOT NULL,\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u5b9e\u73b0\u4ee3\u7801\uff0c\u9700\u8981\u628amysql\u7684\u7528\u6237\u540d\u3001\u5bc6\u7801\u3001IP\u3001\u7aef\u53e3\u505a\u76f8\u5e94\u4fee\u6539<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\nfrom sqlalchemy import Column, Integer, String\nfrom sqlalchemy.ext.declarative import as_declarative, declared_attr\n\nfrom sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker, Session\n\n# 1\u3001\u521b\u5efa mysql \u8fde\u63a5\u6c60\nsql_a_uri = \"mysql+pymysql:\/\/username:password@127.0.0.1:3306\/learn?charset=utf8mb4\"\nmysql_engine = create_engine(sql_a_uri , pool_size=2, pool_pre_ping=True)\nmysql_session = sessionmaker(autocommit=False, autoflush=False, bind=mysql_engine)\n\n# 2.1\u3001\u521b\u5efa\u57fa\u7c7b\n@as_declarative()\nclass TBLBase:\n    __name__: str \n\n    # \u6839\u636e\u7c7b\u540d\u5b57\uff0c\u81ea\u52a8\u751f\u6210 __tablename__ \u5c5e\u6027\n    @declared_attr\n    def __tablename__(cls) -> str:\n        return cls.__name__.lower()\n\n# 2.2\u3001\u57fa\u4e8e\u57fa\u7c7b\u521b\u5efa\u8868\u5bf9\u5e94\u7684\u5bf9\u8c61\nclass Ntbl(TBLBase) :\n    id = Column(Integer, primary_key=True, index=True, autoincrement=True)\n    name = Column(String(45), nullable=False)\n    age = Column(Integer, nullable=False)\n\n# 3.1\u3001\u521b\u5efa\u589e\u5220\u6539\u67e5\u57fa\u7c7b\nclass CRUDBase() :\n    def add_one(self, one_data ) :\n        with mysql_session() as db :\n            db.add(one_data)\n            db.commit()\n\n# 3.2\u3001\u521b\u5efa\u57fa\u4e8e\u6b64\u8868\u7684\u589e\u5220\u6539\u67e5\u5904\u7406\u7c7b\nclass CRUDNtbl(CRUDBase) :\n    pass\n\n\n# 4\u3001\u6d4b\u8bd5\nif __name__ == \"__main__\" :\n    one_ntbl = Ntbl(name=\"name02\", age=10)\n\n    crud_ntbl = CRUDNtbl()\n    crud_ntbl.add_one(one_ntbl)<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>sqlalchemy\u65b9\u5f0f\u5bf9mysql\u5904\u7406<\/p>\n","protected":false},"author":1,"featured_media":548,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[155,154],"tags":[91,142,189],"class_list":["post-582","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-154","tag-mysql","tag-python","tag-sqlalchemy"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=582"}],"version-history":[{"count":4,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/582\/revisions"}],"predecessor-version":[{"id":586,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/582\/revisions\/586"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/548"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}