speedup by switching from engine to connection/transaction

I originally used the sqlalchemy 'engine' object to execute all the queries
without realizing this was opening/closing a connection each time, and was
terribly slow. The performance hit only showed up when I switched from :memory:
to an actual file. This commit changes to using a persistent connection, and
furthermore wraps each batch of inserts/updates in a transaction.