flask_sqlalchemy 根据mysql表生成model

import os

def create_models():

db_url = "mysql+pymysql://root:password@localhost:3306/test?charset=utf8mb4"
project_path = os.getcwd()
print(project_path)
model_path = os.path.join(project_path, 'models.py')
cmd = 'flask-sqlacodegen --flask {}'.format(db_url)
try:
output = os.popen(cmd)
resp = output.buffer.read().decode(encoding='utf-8')
content = str(resp)
output.close()
# w+ 读写权限
with open(model_path, 'w+', encoding='utf-8') as f:
f.write(content)
print('create models successfully!')
except Exception as e:
print(e)
if __name__ == '__main__':

create_models()

pip install flask-sqlacodegen 
pip install pymysql
pip install flask-sqlalchemy
posted @ 2022-05-18 16:58  wyz_1  阅读(85)  评论(0编辑  收藏  举报