2012-11-06 41 views
6

我認爲這是一個簡單的問題,但我沒有在FMDB git頁面中找到答案。 當您使用命令:SQLite FMDB創建表 - 初學者iOS

[database executeUpdate:@"create table T_table(name text primary key, age int)"]; 

不FMDB SQLite的或做出某種驗證,看看錶已經存在?

我可以在我的類初始化程序中調用此方法而不創建多個表嗎?

對不起,如果愚蠢的問題。

回答

0

只要您將CREATE TABLE命令提供給FMDB,它就會在內部將其轉換爲相應的SQLite查詢(您不必擔心)。

按上SQLite的網站給出的官方文檔,它指出:

"It is usually an error to attempt to create a new table in a database that already contains a table, index or view of the same name."

因此,如果您嘗試創建具有相同名稱的另一個表時,SQLite將拋出一個錯誤,說:

create table test_table (test_no NUMBER, test_name TEXT); //Table created 

/* Now, try creating the table again */ 
create table test_table (test_no NUMBER, test_name TEXT); 

您將收到以下錯誤消息。
錯誤:表TEST_TABLE已經存在

所以,爲表存在SQLite的檢查,它不會允許另一個表具有相同的名稱。

同樣,您可以參考文檔以獲取更多詳細信息。

來源http://www.sqlite.org/lang_createtable.html

+1

謝謝你的詳細解答。 – pedros

+0

很高興能有所幫助。 –

14

另一種解決方案是將您的查詢更改爲:

create table if not exists test_table (test_no NUMBER, test_name TEXT); 

或者,您可以檢查是否存在使用:

select sql from SQLITE_MASTER where name = 'test_table' 

,看看你是否得到任何結果回來。