I highly suggest that you do both tutorials before continuing on your trip with SQLAlchemy. They are really helpful and explain many concepts. Afterwards, I suggest you read Using the Session as this then goes on to explain how the session fits into all of this.
To your problem, there are two solutions: One using the ORM and the other using the Core. The former is easier, the latter is faster. Let's take the easy road first. A transaction is only used to wrap all your statements into a single operation. That is, if something fails, you can abort all of it and are not left with something somewhere in between. So you most likely want a transaction, but it would work without one. Here is the quickest way:
with session.begin():
session.add_all([tableRow(row) for row in listOfRows])
Depending on your data SQLAlchemy might even be able to optimize your INSERT
statement in such a way that it executes multiple at a time. Here is what's going on:
- A transaction is started using
session.begin
- The data is added (using
add_all
, but a loop with multiple add
would also be fine)
- The session is committed. If something goes wrong here, the transaction will be aborted and you can fix the error.
So this is clearly a good way, but it is not the fastest way, because SQLAlchemy has to go through all the ORM algorithms which can produce somewhat of an overhead. If this is a one-time database initialization, you can avoid the ORM. In that case, instead of creating an ORM class (tableRow
), you create a dictionary with all keys (how depends on the data). Again you can use a context manager:
with engine.begin() as connection:
connection.execute(tableRow.__table__.insert().
values([row_to_dict(row) for row in listOfRows]))
This would most likely be slightly faster but also less convenient. It works the same way as the session above only that it constructs the statement from the Core and not the ORM.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…