我如何在sqlalchemy中使用类,我一直在尝试使用类,但表没有创建

问题描述 投票:0回答:1

表未创建,我不想使用 declarative_base 和 alchemy.orm

我被告知要上课,但我不知道如何安排

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)
python class sqlalchemy metadata
1个回答
0
投票

使用声明性基函数创建声明性映射的基类。该基类将用作所有映射类的基类。 为数据库中的每个表定义一个映射类。映射类应该是基类的子类,并使用 Column 对象定义表的列。您还可以使用 table name 属性来指定表的名称。 使用 Base 的 create all 方法。用于在数据库中创建表的元数据对象。 使用会话创建器功能创建用于查询数据库的会话。 使用会话的查询方法来查询数据库并检索映射类的实例。 使用会话的 add 方法将映射类的新实例添加到数据库中。 使用会话的 commit 方法将更改保存到数据库。

© www.soinside.com 2019 - 2024. All rights reserved.