From 69ec13ea81cae146933d8c945646b58b48655838 Mon Sep 17 00:00:00 2001 From: roger Date: Thu, 22 Jun 2023 13:20:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=A7=8B=E5=AD=A6=E4=B9=A0sqlalchemy1?= =?UTF-8?q?.4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlalchemy_1.4/db_schema.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 sqlalchemy_1.4/db_schema.py diff --git a/sqlalchemy_1.4/db_schema.py b/sqlalchemy_1.4/db_schema.py new file mode 100644 index 0000000..fa0039e --- /dev/null +++ b/sqlalchemy_1.4/db_schema.py @@ -0,0 +1,35 @@ +import sqlalchemy +from sqlalchemy.orm import declarative_base, sessionmaker, Session # declarative 声明式 +from sqlalchemy import create_engine, Column, Integer, String + +# 创建数据引擎 +HOST = "home.rogersun.cn" +USER = "root" +PWD = "Sxzgx1209" +PORT = "3306" +DB = "orm_sqlalchemy" + +URI = f"mysql+pymysql://{USER}:{PWD}@{HOST}:{PORT}/{DB}" +engine = create_engine(URI, echo=True) + +# 创建session +session = sessionmaker(engine) + +# with session +# with Session(engine) as db_session: +# session.begin() +# try: +# db_session.add("some_object") +# db_session.add("some_other_object") +# except: +# db_session.rollback() +# raise +# else: +# db_session.commit() + +# 生成orm基类 +Base = declarative_base() + + +class User(Base): + __tablename__ = 'user'