I am trying to do a bulk insert of a large list of dictionaries of the form:
results = [{'attribute': u'SEX', 'value_d': 0.0, 'value_s': u'M', 'sid': 1L},
{'attribute': u'SEX', 'value_d': 0.0, 'value_s': u'M', 'sid': 2L},
{'attribute': u'SEX', 'value_d': 0.0, 'value_s': u'M', 'sid': 3L},
{'attribute': u'SEX', 'value_d': 0.0, 'value_s': u'M', 'sid': 4L},
...
]
After reading about 'executemany' and bulk_insert_mappings, I decided to try the later, since it looked much simpler to work with.
Here is the code to execute this, with the naive assumption that this would work with a list of dictionaries:
Session = sessionmaker(bind=engine)
s = Session()
s.bulk_insert_mappings(Results,results)
My Results model is:
class Results(db.Model):
__tablename__ = 'results'
id = Column(Integer, primary_key=True, autoincrement=True)
sid = Column(Integer)
attribute = Column(String(2048))
value_s = Column(String(2048))
value_d = Column(Float)
db is the SQLAlchemy object for my app:
db = SQLAlchemy(app)
No errors are thrown when I run this, but the data are not being inserted.
End of naive assumption: The documentation says I need a "list of mapping dictionaries." I assume since my dictionaries are key-value pairs, I need to do something like
dict = {k:v for k,v in (x.split(':') for x in results) }
I tried this, but I then got an error that
AttributeError: 'dict' object has no attribute 'split'
Not sure where to go with this now...
Thanks in advance!
Greg--