2010-10-28 45 views
2

hai guys, 我打算創建一個表視圖和相應行的詳細視圖,所有的數據都是從sqlite數據庫中獲取的。爲此,我在數據庫表中創建了兩個表,一個用於表視圖列表中的數據,另一個用於表列表的詳細視圖。我將產品ID設置爲表格的主鍵。我想要產品ID作爲表二的外鍵。我不知道如何爲它設置外鍵,也不知道如何從表2中檢索並在詳細視圖中顯示它。請幫我做到這一點。 在此先感謝...在iPhone SDK中的sqlite外鍵

回答

3
1.Change your DB Settings enable to Foreign Keys. 

2.Create the child table like this 


    table1 is the parent table having id1 as primary key. 

    CREATE TABLE "table1" ("id1" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) 

    table2 is the child table having id2 as a foreign key with reference to id1 of table1. 

CREATE TABLE table2 ( 
     id2   INTEGER, 
     parent_id INTEGER, 
     description TEXT, 
     FOREIGN KEY (id2) REFERENCES table1(id1) 
) 

使用equijoin從表中檢索數據。

select * from table1,table2 where table1.id1=table2.id2; 

or 

select table2.* from table2,table1 where table1.id1=table2.id2; 

    to retrieve the data from single table alone. 

希望這對你有幫助!

+0

@iphony:當我試圖通過使用你的代碼從表中檢索數據時,沒有任何迴應。檢索到的數據沒有顯示,因爲只有一個空白區域。 – 2012-11-16 09:46:03