Bulk insert using bulk_insert_mappings

807 views
Skip to first unread message

Horcle

unread,
Dec 3, 2015, 11:28:50 AM12/3/15
to sqlalchemy
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--

Mike Bayer

unread,
Dec 3, 2015, 11:48:45 AM12/3/15
to sqlal...@googlegroups.com
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 sqlalchemy+...@googlegroups.com
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Horcle

unread,
Dec 3, 2015, 11:52:04 AM12/3/15
to sqlalchemy
Sweet! It was the commit.

Thanks!

Greg--
Reply all
Reply to author
Forward
0 new messages