23d4713039859a86d7b3ec18d3cea563000562b8
[webpysample.git] / db.py
1 from sqlalchemy import *
2 from sqlalchemy.ext.declarative import declarative_base
3 from sqlalchemy.orm import sessionmaker, scoped_session
4
5 Base = declarative_base()
6 metadata = Base.metadata
7
8 class Test(Base):
9     __tablename__ = 'test'
10
11     id = Column(Integer, primary_key=True)
12     test = Column(String(128))
13     atime = Column(DateTime(), nullable=False, server_default=text('current_timestamp'))
14
15     def __init__(self, test):
16         self.test = test
17
18 def connect(database='sqlite:///database.sqlite'):
19     db = create_engine(database, echo=False)
20     session = scoped_session(sessionmaker(bind=db))
21     return session
22
23 def create(database='sqlite:///database.sqlite'):
24     db = create_engine(database, echo=False)
25     metadata.create_all(db)
26
27 if __name__ == '__main__':
28     create()
29