class GetModels:
def __init__(self,user_name, password, host, port, db_name, file_path):
self.user_name = user_name
self.password = password
self.host = host
self.port = port
def get_models(self, db_name, file_path):
cmd = f'''sqlacodegen mysql+pymysql://{self.user_name}:{self.password}@{self.host}:{self.port}/{db_name} > {file_path} '''
os.system(cmd)
def run(self):
conn = pymysql.connect(host=self.host,
port=int(self.port),
user=self.user_name,
password=self.password,
database=self.db_name,
charset="utf8",
connect_timeout=180)
cur = conn.cursor()
sql = f''' select b.SCHEMA_NAME from information_schema.SCHEMATA b where schema_name="{self.db_name}" '''
cur.execute(sql)
db_names = cur.fetchall()
for db_name in db_names:
file_path = f'{db_name}/{db_name[0]}.py'
self.get_models(db_name, file_path)
sqlacodegen 批量生成models
于 2024-01-27 17:01:16 首次发布