On 12/03/2015 11:28 AM, Horcle wrote:
> 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.
1. are you emitting s.commit() to commit your transaction?
2. what's in that "results" object? it should definitely do something
unless the collection is empty.
3. turn on echo=True on your engine to see what SQL is emitted.
Also note that the "SQLAlchemy(app)" object there is a flask thing, that
provides its own Session. There's not much reason for it if you are
making a Session() yourself with an "engine" from somewhere.
>
> 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--
>
> --
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.