Следующий (автономный рабочий пример) код должен дать вам идею:
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData
from sqlalchemy.orm import mapper, sessionmaker
engine = create_engine('sqlite:///:memory:', echo=True,)
metadata = MetaData()
mytable = Table('mytable', metadata,
Column('id', Integer, primary_key=True),
Column('title', String(50)),
Column('description', String(50)),
)
class MyObject(object):
# 1. define keywork based constructor (or do this in the part 2.)
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
mapper(MyObject, mytable)
metadata.create_all(engine)
session = sessionmaker(bind=engine, autoflush=False)()
input = ([('title', 'A title'), ('description', 'A description'),
('title', 'Another title'), ('description', 'Another description'),
('title', 'One more title'), ('description', 'One more description')])
# 2. add data to the session, then commit
while input:
values = dict((input.pop(0), input.pop(0)))
obj = MyObject(**values)
session.add(obj)
session.commit()