如何在SQLAlchemy中使用类?我尝试使用类但表没有创建

-2 投票
0 回答
31 浏览
提问于 2025-04-12 09:23

我想创建一个表格,但不想使用 declarative_basesqlalchemy.orm 这些东西。

有人告诉我可以用 class 来实现,但我不知道该怎么安排它。

class Tables():
    def __init__(self, engine, metadata, table, name, age, location):
        self.engine = sqlengine
        self.metadata = sql_metadata
        self.table = table_name
        self.name = data_name
        self.age = data_age
        self.location = data_location

    def make_table(self):
        with self.engine.connect() as connection:
            member = Table(self.table, self.metadata,
                           Column("id", Integer, primary_key=True),
                           Column(self.name, String, unique=True),
                           Column(self.age, Integer),
                           Column(self.location, String)
                           )
        sql_metadata.create_all(sqlengine)

        with sqlengine.connect() as connection:
            sql_metadata.reflect(bind=sqlengine)
            for table in sql_metadata.tables.values():
                print(table)

0 个回答

暂无回答

撰写回答