如何在Android中插入外键?

时间:2014-06-18 09:38:26

标签: java android android-sqlite

我必须创建2到3个表,并且我想创建两个表的外键并添加到第3个表中。当我运行应用程序时,数据插入成功,但外键未显示在数据库表结构和表中。

这是我的数据库类:

     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;

   }

1 个答案:

答案 0 :(得分: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+")";