Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate a SQLite table if it exists?

Tags:

sqlite

To truncate a table in SQLite I need to use this syntax:

DELETE FROM someTable 

But how do I truncate the table only if it exists?

Unfortunately this throws an error:

DELETE FROM someTable IF EXISTS 

This doesn't work either:

DELETE IF EXISTS FROM someTable 

Thanks.

like image 951
Dan Avatar asked Nov 25 '10 18:11

Dan


People also ask

How can you delete the existing records from a table in SQLite?

SQLite DELETE query is used to remove existing records from a specified table. You can use the WHERE clause with DELETE queries to delete the selected rows. You have to write a table name after the DELETE FROM clause, from which you want to delete records.

Can you remove any table at any time in SQLite?

SQLite allows you to drop only one table at a time. To remove multiple tables, you need to issue multiple DROP TABLE statements. If you remove a non-existing table, SQLite issues an error.

How do you delete a table in SQLite database explain with an example?

SQLite DELETE Query is used to delete the existing records from a table. You can use WHERE clause with DELETE query to delete the selected rows, otherwise all the records would be deleted.

How do I reduce the size of my SQLite database?

SQLite is somewhat lazy to reclaim unused space; use the VACUUM command or auto vacuum mode. the datbase format trades space efficiency for speedy access. if you just dump the data contents, either as SQL dump or simply a CVS file for each database table, you'll likely get smaller files.


1 Answers

It is the two step process:

  1. Delete all data from that table using:

    Delete from TableName 
  2. Then:

    DELETE FROM SQLITE_SEQUENCE WHERE name='TableName'; 
like image 96
iKushal Avatar answered Dec 31 '22 18:12

iKushal