SHARE
TWEET

Untitled

a guest Jan 25th, 2016 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from sqlalchemy import create_engine
  2. from sqlalchemy.ext.declarative import declarative_base
  3. from sqlalchemy import Column, Integer, String
  4. from sqlalchemy.orm import sessionmaker
  5.  
  6. engine = create_engine('sqlite:///:memory:', echo=True)
  7. Base = declarative_base()
  8.  
  9. Session = sessionmaker(bind=engine)
  10. session = Session()
  11.  
  12. class User(Base):
  13.     __tablename__ = 'users'
  14.     id       = Column(Integer, primary_key=True)
  15.     name     = Column(String)
  16.     fullname = Column(String)
  17.     password = Column(String)
  18.  
  19.     def __repr__(self):
  20.         return "User<name={}, fullname={}, password={}>".format(self.name, self.fullname, self.password)
  21.  
  22. # Create all tables
  23. Base.metadata.create_all(engine)
  24.  
  25. ed_user = User(name='ed', fullname='Ed Jones', password='edspassword')
  26. session.add(ed_user)
  27. session.add_all([
  28.     User(name='wendy', fullname='Wendy Williams', password='foobar'),
  29.     User(name='mary', fullname='Mary Contrary', password='xxg527'),
  30.     User(name='fred', fullname='Fred Flinstone', password='blah')
  31. ])
  32. ed_user.password = 'f8s7ccs'
  33. print(session.dirty) # Note: session.dirty without print does nothing
  34.    
  35. 2016-01-26 07:57:43,149 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1
  36. 2016-01-26 07:57:43,149 INFO sqlalchemy.engine.base.Engine ()
  37. 2016-01-26 07:57:43,150 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1
  38. 2016-01-26 07:57:43,150 INFO sqlalchemy.engine.base.Engine ()
  39. 2016-01-26 07:57:43,151 INFO sqlalchemy.engine.base.Engine PRAGMA table_info("users")
  40. 2016-01-26 07:57:43,151 INFO sqlalchemy.engine.base.Engine ()
  41. 2016-01-26 07:57:43,152 INFO sqlalchemy.engine.base.Engine
  42. CREATE TABLE users (
  43.     id INTEGER NOT NULL,
  44.     name VARCHAR,
  45.     fullname VARCHAR,
  46.     password VARCHAR,
  47.     PRIMARY KEY (id)
  48. )
  49.  
  50.  
  51. 2016-01-26 07:57:43,152 INFO sqlalchemy.engine.base.Engine ()
  52. 2016-01-26 07:57:43,152 INFO sqlalchemy.engine.base.Engine COMMIT
  53. IdentitySet([])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top