from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker engine = create_engine('mysql+pymysql://', connect_args={ 'user': "root", 'password': "12341234", 'host': '10.10.10.200', 'port': 3306, 'database': 'car', 'charset': 'utf8mb4' }) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) def database(): _db = SessionLocal() try: yield _db finally: _db.close()