How to insert multiple rows in SQLite Dbase
Gerhard Häring
gh at ghaering.de
Sat Mar 29 14:46:19 EDT 2008
Gabriel Genellina wrote:
> [...]
> and execute:
> cur.executemany("insert into log (IP, EntryDate, Requestt, ErrorCode)
> values (:ip, :date, :request, :errorcode)", values)
It's probably worth mentioning that pysqlite's executemany() accepts
anything iterable for its parameter. So you don't need to build a list
beforehand to enjoy the performance boost of executemany().
The deluxe version with generators could look like this:
def parse_logfile():
logf = open(...)
for line in logf:
if ...:
row = (value1, value2, value3)
yield row
logf.close()
...
cur.executemany("insert into ... values (c1, c2, c3)", parse_logfile())
-- Gerhard
PS: pysqlite internally has a statement cache since verson 2.2, so
multiple execute() calls are almost as fast as executemany().
More information about the Python-list
mailing list