Loop in C, not in Python
executemany takes a SQL statement and an iterable of parameter tuples. The driver loops over them in C, reusing the prepared statement. For bulk inserts, this is dramatically faster than a Python for loop calling execute per row — and it pairs naturally with a single transaction.
Three rules:
- Use it whenever you have >= 100 rows to write.
- Wrap it in a transaction (
with conn:). - For very large batches, chunk into 1k–10k rows to keep memory and lock duration sane.
Tip:
executemany + transaction is usually the difference between 'inserts feel slow' and 'inserts feel free'. The most common SQLite-is-slow report is missing transactions, not anything else.