从两个不同的py文件在SQLAlchemy中创建表

2024-04-19 20:04:50 发布

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

我有两个python文件,Data.pySql.py,我想让SQLAlchemy用一个表创建一个数据库,数据库的名称取自Data.py

ie:一个数据库,其名称取自另一个.py文件

在Data.py中编码

class ddd():
    which_mkt = 'US, HK'
    # user input
    mkt = input("Choose a value? [" + which_mkt + "]").upper()
    t = os.chdir(DirectoryListings.T_dir) # Check in folder if file exist

    if os.path.isfile(t):
        # code to create a .db database file with mkt as the name. 

Sql.py中的代码

# Create a database engine
engine = create_engine(r"sqlite:///Data\\" + z + ".db", echo=True)
Base = declarative_base()

class Gott(Base):
    __tablename__ = 'Country'
    id = Column(Integer, primary_key=True)
    Country = Column(String(255))

    def __init__(self, country):
        self.Country = country

Base.metadata.create_all(engine)

因此,在Sql.py中,z应该是取自Data.py的数据库的变量名

我希望我的解释清楚。这应该怎么做


Tags: 文件py名称数据库whichinputsqldata