this is likely a common and easy answer, so my <blushes> for not being able to figure it out.
How do I test for the existence of a table or a view so I can do something like...


if <table|view> exists
then DROP <table|view>
else CREATE <table|view> ()...

what I do in such situations is


SELECT 1 FROM sqlite_master WHERE type='table' AND name ='whatever';

which selects a single "1" row or nothing. A more portable way (between different SQL implementations) might be

SELECT 1 FROM tablename WHERE 1 == 0

If this fails the tablename table doesn't exist, otherwise it exists.

The problem of generating a potentially existing table can obviously be tackled by exec'ing the CREATE TABLE statement, and catching the "table already exists" error in case it occurs.


What about such a script? It's a bit tricky but it should work...

*****************
CREATE TEMP TABLE fakeTable (exists);
INSERT INTO fakeTable (exists) VALUES (2);

CREATE TEMP TRIGGER fakeTrigger INSERT ON fakeTable
        BEGIN
                DROP 'tablename';
                DELETE FROM fakeTable WHERE exists = 2;
        END;

INSERT INTO fakeTable SELECT 1 FROM sqlite_master WHERE type='table' AND name ='tablename';

CREATE TEMP TRIGGER fakeTrigger2 DELETE ON fakeTable
        BEGIN
                CREATE 'tablename' ........;
        END;

DELETE FROM fakeTable WHERE exists = 2 AND count(SELECT 1 FROM sqlite_master WHERE type='table' AND name ='tablename') = 0;

DROP trigger fakeTrigger2;
DROP trigger fakeTrigger;
DROP table fakeTable;
*********************

bye,
Paolo




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to