|
@@ -1,2 +1,39 @@
|
|
|
# sqlalchemy
|
|
|
|
|
|
+数据库orm框架,
|
|
|
+
|
|
|
+类似项目:
|
|
|
+
|
|
|
+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)
|
|
|
+
|
|
|
+# 查询
|
|
|
+dept_info = SessionLocal.query(User) \
|
|
|
+ .filter(User.status == 0,
|
|
|
+ User.del_flag == 0) \
|
|
|
+ .first()
|
|
|
+
|
|
|
+```
|
|
|
+
|