使用peewee+sqlite批量插入資料報錯
peewee.OperationalError: too many SQL variables
sqlite 每次插入有數量限制,不能插入太多,資料顯示999,不過我設定為500就報錯了
peewee提供了一個方式,分次插入,兩種方式等價
from peewee import chunked
# Insert rows 100 at a time.
with db.atomic():
for idx in range(0, len(data_source), 100):
MyModel.insert_many(data_source[idx:idx+100]).execute()
# Insert rows 100 at a time.
with db.atomic():
for batch in chunked(data_source, 100):
MyModel.insert_many(batch).execute()
參考: