gpt4 book ai didi

java - Android中如何插入外键?

转载 作者:太空宇宙 更新时间:2023-11-04 14:47:01 24 4
gpt4 key购买 nike

我必须创建 2 到 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;

}

最佳答案

+ " 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+")";

关于java - Android中如何插入外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24282131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com