数据库orm框架 https://github.com/sqlalchemy/sqlalchemy

天问 a50d108def Update 'README.md' 3 days ago
README.md a50d108def Update 'README.md' 3 days ago

README.md

sqlalchemy

数据库orm框架,支持 MySQL、PostgreSQL、SQLite、Oracle

类似项目:

sqlmodel,

Usage

# 创建一个 SQLAlchemy 模型
from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine(
    "mysql+pymysql://test:123456@192.168.1.1:3306/test?charset=utf8", echo=True
)

# SessionLocal 实例用来操作数据库
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# 表需要继承 Base
Base = declarative_base()

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    username = Column(String(50), unique=True)
    email = Column(String(100), unique=True)

# 初始化表
Base.metadata.create_all(bind=engine)

# 查询
dept_info = SessionLocal.query(User) \
    .filter(User.status == 0,
            User.del_flag == 0) \
    .first()