安装sqlacodegen
pip install sqlacodegen
安装依赖
centos: sudo yum install python-devel postgresql-devel
unbtu: sudo apt-get install libpq-dev python-dev
安装psycopg2
pip install psycopg2
test.py:
import os
import subprocess
def run():
host = ''
user = ''
password = ''
port = 3306
db = ''
args = f'sqlacodegen --noviews --outfile ./model.py postgresql://{user}:{password}@{host}:{port}/{db}'
os.system(args)
print('写入完成')
if __name__ == '__main__':
run()