天天看點

peewee.OperationalError: too many SQL variables

使用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()      

參考:

  1. How to increase SQLITE_MAX_VARIABLE_NUMBER at compilation-time?
  2. http://docs.peewee-orm.com/en/latest/peewee/querying.html#bulk-inserts