2014-06-18 65 views
1

我必須創建2到3個表,我想創建兩個表的外鍵並添加到第3個表中。當我運行應用程序數據插入成功,但外鍵不顯示在數據庫表結構和表。如何在Android中插入外鍵?

這裏是我的數據庫類:

 public static final String Tsk_id = "task_id"; 
    public static final String Task_Project_NAme = "project_name"; 
    public static final String Task_Team_Member_Name = "assign_to"; 
    public static final String Task_Title = "task_time"; 
    public static final String Task_Start_Date = "start_date"; 
    public static final String Task_CompletionDate = "completion_date"; 
    public static final String Task_CompletionTime = "completion_time"; 
    public static final String Task_Description = "task_description"; 
    public static final String Task_Status = "task_status"; 
    public static final String Task_Is_Active = "IsActive"; 
    public static final String Task_Project_id="project_id"; 
    public static final String Task_Team_Memmber_Id="team_member_id"; 

@Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 



     String CREATE_TASK_TABLE = "CREATE TABLE " + ASSIGN_TASK_TABLE + "(" 
           + Tsk_id +" integer primary key autoincrement, " 
           + Task_Project_NAme + " TEXT," 
           + Task_Team_Member_Name + " TEXT," 
           + Task_Title + " TEXT," 
           + Task_Start_Date + " TEXT," 
           + Task_CompletionDate + " TEXT," 
           + Task_CompletionTime + " TEXT," 
           + Task_Description + " TEXT," 
           + Task_Status + " TEXT," 
           + Task_Is_Active + " TEXT" 
           + " FOREIGN KEY ("+Task_Project_id+") REFERENCES " +CONTACTS_TABLE_NAME+" ("+Project_id+")" 
           + " FOREIGN KEY ("+Task_Team_Memmber_Id+") REFERENCES " +DEFINE_TEAM_MEMBER_TABLE+" ("+Team_Member_id+")"; 
     db.execSQL(CREATE_TASK_TABLE); 

    } 



public boolean insertTaskDetails(String strTask_Project_NAme,String strTask_Team_Member_Name, 
            String strTask_Title,String strTask_Start_Date,String strTask_CompletionDat, 
            String strTask_CompletionTime,String strTask_Description, 
            String strTask_Status,String strTask_Is_Active) 
    { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 

     contentValues.put(Task_Project_NAme, strTask_Project_NAme); 
     contentValues.put(Task_Team_Member_Name, strTask_Team_Member_Name); 
     contentValues.put(Task_Title, strTask_Title); 
     contentValues.put(Task_Start_Date, strTask_Start_Date); 
     contentValues.put(Task_CompletionDate,strTask_CompletionDat); 
     contentValues.put(Task_CompletionTime,strTask_CompletionTime); 
     contentValues.put(Task_Description, strTask_Description); 
     contentValues.put(Task_Status, strTask_Status); 
     contentValues.put(Task_Is_Active,strTask_Is_Active); 

     db.insert(ASSIGN_TASK_TABLE, null, contentValues); 
     db.close(); 
     return true; 

    } 

回答

0
+ " FOREIGN KEY ("+Task_Project_id+") REFERENCES " +CONTACTS_TABLE_NAME+" ("+Project_id+")" 

此行的代碼,其中在在創建表Task_Project_id列..請檢查。

您需要創建一個特定的列作爲外鍵引用。

同這一行太

" FOREIGN KEY ("+Task_Team_Memmber_Id+") REFERENCES " +DEFINE_TEAM_MEMBER_TABLE+" ("+Team_Member_id+")";