使用SQLAlchemy添加用户并限制计数

2024-05-18 08:44:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

def start() -> scoped_session:
    engine = create_engine(os.environ.get(DB_URL))
    base.metadata.create_all(engine)
    return scoped_session(sessionmaker(bind=engine, autoflush=False))

base = declarative_base()
session = start()

和另一个代码:

class limit(base):
    __tablename__ = 'req_limit'
    user_id = Column(Integer, primary_key=True)
    limit_count = Column(Integer, default=0)

如何从一个变量中获取用户,从另一个变量中获取限制计数,并将其添加到数据库中

我想将它们添加到列中,然后将它们取出,它们需要以某种方式链接(用户和id),因此在此之后,我将能够获取限制并基于用户名在控制台中显示限制

 user = 'ABC'
 limit_count = 1

Tags: 代码用户idbasesessiondefcountcreate