python - Sqlalchemy if table does not exist -
i wrote module create empty database file
def create_database(): engine = create_engine("sqlite:///myexample.db", echo=true) metadata = metadata(engine) metadata.create_all()
but in function, want open myexample.db
database, , create tables if doesn't have table.
eg of first, subsequent table create be:
table(variable_tablename, metadata, column('id', integer, primary_key=true, nullable=false), column('date', date), column('volume', float))
(since empty database, have no tables in it, subsequently, can add more tables it. thats i'm trying say.)
any suggestions?
i've managed figure out intended do. used engine.dialect.has_table(engine, variable_tablename)
check if database has table inside. if doesn't, proceed create table in database.
sample code:
engine = create_engine("sqlite:///myexample.db") # access db engine if not engine.dialect.has_table(engine, variable_tablename): # if table don't exist, create. metadata = metadata(engine) # create table appropriate columns table(variable_tablename, metadata, column('id', integer, primary_key=true, nullable=false), column('date', date), column('country', string), column('brand', string), column('price', float), # implement creation metadata.create_all()
this seems giving me i'm looking for.
Comments
Post a Comment