diff --git a/docs/python-api.rst b/docs/python-api.rst index 2a87610..4d6676a 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -678,6 +678,19 @@ Here's an example that uses these features: # [score] INTEGER NOT NULL DEFAULT 1 # ) +.. _python_api_duplicate: + +Duplicating tables +================== + +The ``table.duplicate()`` method creates a copy of the table, copying both the table schema and all of the rows in that table: + +.. code-block:: python + + db["authors"].duplicate("authors_copy") + +The new ``authors_copy`` table will now contain a duplicate copy of the data from ``authors``. + .. _python_api_bulk_inserts: Bulk inserts diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 5d4d979..3a27cbe 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -1476,20 +1476,20 @@ class Table(Queryable): ) return self - def duplicate(self, name_new: str) -> "Table": + def duplicate(self, new_name: str) -> "Table": """ - Duplicate this table in this database. + Create a duplicate of this table, copying across the schema and all row data. - :param name_new: Name of new table. + :param new_name: Name of the new table """ assert self.exists() with self.db.conn: sql = "CREATE TABLE [{new_table}] AS SELECT * FROM [{table}];".format( - new_table=name_new, + new_table=new_name, table=self.name, ) self.db.execute(sql) - return self.db[name_new] + return self.db[new_name] def transform( self,