Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sqlalchemy import Column, String, Integer, create_engine
- from sqlalchemy.orm import relationship, sessionmaker
- from sqlalchemy.ext.declarative import declarative_base
- Base = declarative_base()
- class Person(Base):
- __tablename__ = 'person'
- id = Column(Integer, primary_key = True)
- name = Column(String(250), nullable=False)
- job = Column(String(250), nullable=False)
- engine = create_engine('sqlite:///persons.sqlite')
- session = sessionmaker()
- session.configure(bind=engine)
- Base.metadata.create_all(engine)
- dct = {'John':'doctor', 'Alice':'typist'}
- for el in dct:
- np = Person(name=el, job=dct[el])
- s = session()
- s.add(np)
- s.commit()
- from sqlalchemy import Column, String, Integer, create_engine
- from sqlalchemy.orm import sessionmaker
- from sqlalchemy.ext.declarative import declarative_base
- Base = declarative_base()
- class Person(Base):
- __tablename__ = 'person'
- id = Column(Integer, primary_key=True)
- name = Column(String(250), nullable=False)
- job = Column(String(250), nullable=False)
- engine = create_engine('sqlite:///persons.sqlite')
- Base.metadata.create_all(engine)
- session = sessionmaker(bind=engine)
- s = session()
- dct = {'John': 'doctor', 'Alice': 'typist'}
- for key, value in dct.items():
- np = Person(name=key, job=value)
- s.add(np)
- s.commit()
- db_result = s.query(Person).all()
- for row in db_result:
- print row.id, row.name, row.job
- 1 John doctor
- 2 Alice typist
- s = session()
- for name, job in dct.iteritems():
- np = Person(name=name, job=job)
- s.add(np)
- s.commit()
- session.execute(Person.__table__.insert(), dict_values))
- session.commit()
Add Comment
Please, Sign In to add comment