chacham15
chacham15

Reputation: 14281

How can I list temporary tables/views in SQLite?

I have a function which attempts to drop a view and rebuild it whenever a new database is attached to the connection. The problem (well, more of an annoyance) is that the first time it tries to drop a non existent view. Since there is a permanent table with the same name as the view I get the sqlite error: use DROP TABLE to delete table Albums. Therefore, I would like to query the database to check if the view exists before attempting to drop it.

Note: the current query already uses DROP VIEW IF EXISTS and that does not solve the problem. Also, I checked sqlite_master and there doesnt appear to be any references to temporary tables/views.

Upvotes: 2

Views: 1950

Answers (1)

CL.
CL.

Reputation: 180270

The main and temp databases are distinct, so you can just use the database name:

CREATE TABLE Albums(x);
CREATE TEMPORARY VIEW Albums AS SELECT * FROM Albums;
DROP VIEW IF EXISTS temp.Albums;
DROP VIEW IF EXISTS temp.Albums;

Temporary objects are managed in sqlite_temp_master.

Upvotes: 2

Related Questions