Created
September 13, 2012 10:34
-
-
Save tell-k/3713469 to your computer and use it in GitHub Desktop.
This file contains 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
# テーブルを表現するオブジェクト | |
entry_table = Table( | |
'entries', metadata, | |
Column("id", Integer, primary_key=True), | |
Column("text", String(200)), | |
Column("created_at", DateTime, default=datetime.now, nullable=False) | |
) | |
# Tableオブジェクトとマッピングされるクラス | |
class Entry(object): | |
def __init__(self, id, text, created_at=None): | |
self.id = id | |
self.text = text | |
self.created_at = created_at | |
Entry.metadata = metadata | |
Entry.query= db_session.query_property() | |
# EntryクラスとTableオブジェクトをマッピング | |
mapper(Entry, entry_table) | |
Entry.metadata.create_all() # テーブル作成 | |
entry = Entry(id=1, text="entrytext") # オブジェクト生成 | |
db_session.add(entry) # DBにインサート | |
db_session.commit() | |
entry = Entry.query.first() # データの取得 | |
print entry.text # => "entrytext" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment