当前位置:   article > 正文

Sanic学习笔记-ORM框架SQLAlchemy集成_sanic orm

sanic orm

Python版本:Python-3.9.4

安装SQLAlchemy

pip3 install sqlalchemy

在extension配置中添加相关配置

  1. import os
  2. import logging.config
  3. from sanic_redis import SanicRedis
  4. from sqlalchemy import create_engine
  5. PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  6. # Log
  7. logger = logging.getLogger("sanic.root")
  8. # Sqlite
  9. sqlite_engine = create_engine('sqlite:///' + os.path.join(PROJECT_DIR, 'database', 'platform.db'), encoding='utf-8')
  10. # Redis
  11. redis = SanicRedis()
  12. __all__ = ['logger', 'sqlite_engine', 'redis']

模块中model层使用简单示例

  1. from sqlalchemy import DateTime
  2. from sqlalchemy import Float
  3. from sqlalchemy import Text
  4. from sqlalchemy.ext.declarative import declarative_base
  5. from sqlalchemy.orm import sessionmaker
  6. from app.extension import sqlite_engine
  7. from sqlalchemy import Column, Integer, String
  8. Base = declarative_base()
  9. class TaoBaoKeGoods(Base):
  10. # 表名称
  11. __tablename__ = 'T_TBK_GOODS'
  12. id = Column('ID', Integer, primary_key=True, autoincrement=True)
  13. category_id = Column('CATEGORY_ID', Integer(), nullable=True)
  14. item_id = Column('ITEM_ID', Integer(), nullable=True)
  15. item_description = Column('ITEM_DESCRIPTION', String(length=255), nullable=True)
  16. title = Column('TITLE', String(length=255), nullable=True)
  17. sub_title = Column('SUB_TITLE', String(length=255), nullable=True)
  18. user_type = Column('USER_TYPE', Integer(), nullable&#
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/136972?site
推荐阅读
相关标签
  

闽ICP备14008679号