mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
Add insert --truncate option
Deletes all rows in the table (if it exists) before inserting new rows. SQLite doesn't implement a TRUNCATE TABLE statement but does optimize an unqualified DELETE FROM. This can be handy if you want to refresh the entire contents of a table but a) don't have a PK (so can't use --replace), b) don't want the table to disappear (even briefly) for other connections, and c) have to handle records that used to exist being deleted. Ideally the replacement of rows would appear instantaneous to other connections by putting the DELETE + INSERT in a transaction, but this is very difficult without breaking other code as the current transaction handling is inconsistent and non-systematic. There exists the possibility for the DELETE to succeed but the INSERT to fail, leaving an empty table. This is not much worse, however, than the current possibility of one chunked INSERT succeeding and being committed while the next chunked INSERT fails, leaving a partially complete operation.
This commit is contained in:
parent
f8277d0fb9
commit
ae4593316c
5 changed files with 53 additions and 1 deletions
|
|
@ -423,6 +423,9 @@ The function can accept an iterator or generator of rows and will commit them ac
|
|||
|
||||
You can skip inserting any records that have a primary key that already exists using ``ignore=True``. This works with both ``.insert({...}, ignore=True)`` and ``.insert_all([...], ignore=True)``.
|
||||
|
||||
You can delete all the existing rows in the table before inserting the new
|
||||
records using ``truncate=True``. This is useful if you want to replace the data in the table.
|
||||
|
||||
.. _python_api_insert_replace:
|
||||
|
||||
Insert-replacing data
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue