2009-09-19 65 views

回答

7

要檢測一個特定的表存在,使用方法:

SELECT name 
    FROM sqlite_master 
WHERE type = 'table' 
    AND name LIKE '%your_table_name%' 
+0

更好的答案,因爲它會檢查類型。 – SecretDeveloper 2009-09-19 21:09:09

+0

我認爲檢查類型沒有任何優勢。如果現有的索引或觸發器具有相同的名稱,則仍然無法創建表。 – finnw 2009-09-21 09:56:10

4

有一張名爲sqlite_master的表包含數據庫模式。您可以運行類似的查詢:如果查詢返回1

select count(*) from sqlite_master where name='users';

,表「用戶的存在。您也可以使用SQL if not exists建設:

create table if not exists users (name, pwd);
相關問題