Created
May 17, 2017 09:32
-
-
Save danhper/c0f777ff59718e0682b8a0d2fd2b7e70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlalchemy | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy import Column, Integer, String, Text, DateTime | |
from sqlalchemy.ext.declarative import declarative_base | |
engine = sqlalchemy.create_engine("sqlite:///foo.db", echo=True) | |
Base = declarative_base() | |
@contextmanager | |
def session_scope(session_maker): | |
session = session_maker() | |
try: | |
yield session | |
session.commit() | |
except Exception as e: | |
session.rollback() | |
finally: | |
session.close() | |
class Project(Base): | |
__tablename__ = 'projects' | |
id = Column(Integer, primary_key=True) | |
url = Column(String(255)) | |
title = Column(String(255)) | |
description = Column(Text()) | |
created_at = Column(DateTime()) | |
Base.metadata.create_all(engine) | |
session_maker = sessionmaker(bind=engine) | |
with session_scope(session_maker) as session: | |
print(session.query(Project).all()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment