Skip to content

Instantly share code, notes, and snippets.

@mumumu
Created January 7, 2014 11:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mumumu/8297939 to your computer and use it in GitHub Desktop.
Save mumumu/8297939 to your computer and use it in GitHub Desktop.
sqlalchemy snippet.
#!/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