美文网首页
python数据库操作之sqlalchemy逆向工程

python数据库操作之sqlalchemy逆向工程

作者: ag4kd | 来源:发表于2023-06-19 17:57 被阅读0次
[mysql]
host = 172.16.40.67
port = 3306
password = 11111111
username = root
database = demo
import os

from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
from sqlalchemy.orm import declarative_base, sessionmaker, Session

from config import get_config


if __name__ == '__main__':
    database = 'mysql'
    host = get_config(f'{database}', 'host')
    port = int(get_config(f'{database}', 'port'))
    username = get_config(f'{database}', 'username')
    password = get_config(f'{database}', 'password')
    database_name = get_config(f'{database}', 'database')
    # Mysql版
    DB_URI = f'mysql+pymysql://{username}:{password}@{host}:{port}/{database_name}'

    # SQLite版
    # DB_URI = f'sqlite:///fast.db'

    # 自动生成models
    os.system(f'sqlacodegen {DB_URI} > models.py')

    # 操作数据句柄
    engine: Engine = create_engine(DB_URI)
    Base = declarative_base(engine)
    session: Session = sessionmaker(engine)()

相关文章

网友评论

      本文标题:python数据库操作之sqlalchemy逆向工程

      本文链接:https://www.haomeiwen.com/subject/hanywhtx.html