Python版本:Python-3.9.4
安装SQLAlchemy
pip3 install sqlalchemy
在extension配置中添加相关配置
import os
import logging.config
from sanic_redis import SanicRedis
from sqlalchemy import create_engine
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Log
logger = logging.getLogger("sanic.root")
# Sqlite
sqlite_engine = create_engine('sqlite:///' + os.path.join(PROJECT_DIR, 'database', 'platform.db'), encoding='utf-8')
# Redis
redis = SanicRedis()
__all__ = ['logger', 'sqlite_engine', 'redis']
模块中model层使用简单示例
from sqlalchemy import DateTime
from sqlalchemy import Float
from sqlalchemy import Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from app.extension import sqlite_engine
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class TaoBaoKeGoods(Base):
# 表名称
__tablename__ = 'T_TBK_GOODS'
id = Column('ID', Integer, primary_key=True, autoincrement=True)
category_id = Column('CATEGORY_ID', Integer(), nullable=True)
item_id = Column('ITEM_ID', Integer(), nullable=True)
item_description = Column('ITEM_DESCRIPTION', String(length=255), nullable=True)
title = Column('TITLE', String(length=255), nullable=True)
sub_title = Column('SUB_TITLE', String(length=255), nullable=True)
user_type = Column('USER_TYPE', Integer(), nullable&#