howto create db mysql with sqlalchemy

后端 未结 4 1305
别跟我提以往
别跟我提以往 2020-12-28 12:58

I need to create a db in mysql using sqlalchemy, I am able to connect to a db if it already exists, but I want to be able to create it if it does not exist. this are my tabl

4条回答
  •  温柔的废话
    2020-12-28 13:42

    You can use SQLAlchemy-Utils for that.

    pip install sqlalchemy-utils

    Then you can do things like

    from sqlalchemy_utils import create_database, database_exists
    url = 'mysql://{0}:{1}@{2}:{3}'.format(user, pass, host, port)
    if not database_exists(url):
        create_database(url)
    

    I found the answer here, it helped me a lot.

提交回复
热议问题