Skip to content
Advertisement

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

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')

Does sqlalchemy create an sqlite database for you if one is not already present in a directory it was supposed to fetch the database file?.

Advertisement

Answer

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)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement