Created
January 7, 2014 11:13
-
-
Save mumumu/8297939 to your computer and use it in GitHub Desktop.
sqlalchemy snippet.
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
#!/usr/bin/env python | |
from sqlalchemy import create_engine | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy import Column, Integer, String | |
from sqlalchemy.orm import sessionmaker | |
engine = create_engine('sqlite:///:memory:', echo=True) | |
Session = sessionmaker(bind=engine) | |
Base = declarative_base() | |
class User(Base): | |
__tablename__ = 'users' | |
id = Column(Integer, primary_key=True) | |
name = Column(String) | |
fullname = Column(String) | |
password = Column(String) | |
def __repr__(self): | |
return "<User(name='%s', fullname='%s', password='%s')>" % (self.name, self.fullname, self.password) | |
Base.metadata.create_all(engine) | |
ed_user = User(name='ed', fullname='Ed Jones', password='edspassword') | |
session = Session() | |
session.add(ed_user) | |
session.commit() | |
ed_user.name = 'hoge' | |
session.add(ed_user) | |
session.commit() | |
session.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment