sqlalchemy,creating an sqlite database if it doesn't exist

前端 未结 4 970
日久生厌
日久生厌 2021-02-01 04:05

I am trying out sqlalchemy and i am using this connection string to connect to my databases

engine = create_engine(\'sqlite:///C:\\\\sqlitedbs\\\\database.db\')
         


        
4条回答
  •  别那么骄傲
    2021-02-01 04:26

    Yes,sqlalchemy does create a database for you.I confirmed it on windows using this code

    from sqlalchemy import create_engine, ForeignKey
    from sqlalchemy import Column, Date, Integer, String
    from sqlalchemy.ext.declarative import declarative_base
    
    engine = create_engine('sqlite:///C:\\sqlitedbs\\school.db', echo=True)
    Base = declarative_base()
    
    
    class School(Base):
    
        __tablename__ = "woot"
    
        id = Column(Integer, primary_key=True)
        name = Column(String)  
    
    
        def __init__(self, name):
    
            self.name = name    
    
    
    Base.metadata.create_all(engine)
    

提交回复
热议问题