如何将外键插入表中?我有两个表(Info和WorkDetails),其中WorkDetails有一个引用Info的外键。这是正确的实施方式吗?
MyDatabaseHelper.java
public void onCreate(SQLiteDatabase db)
{
db.execSQL("create table "+TABLE_INFO+"(ID INTEGER PRIMARY KEY ,Name TEXT,Weather TEXT, Date DATETIME, Status Text)");
db.execSQL("create table"+TABLE_WORKDETAILS+"(ID INTEGER PRIMARY KEY , Project TEXT, WorkDescription TEXT, Per Text, TimeIn DATETIME, TimeOut DATETIME,TotalHours DATETIME, TableInfo_id INTEGER, FOREIGN KEY(TableInfo_id)REFERENCES TABLE_INFO(ID)");
}
WorkDetailsAPI.java
public class WorkDetailsAPI {
private SQLiteDatabase database;
private MyDatabaseHelper dbHelper;
public String[] allColumns={MyDatabaseHelper.ID2,MyDatabaseHelper.Project,MyDatabaseHelper.WorkDescription,MyDatabaseHelper.Per,MyDatabaseHelper.TimeIn,MyDatabaseHelper.TimeOut,MyDatabaseHelper.TotalHours,MyDatabaseHelper.TableInfo_id};
public WorkDetailsAPI(Context context)
....
public long insertWorkDetails(String project, String workDescription, String per,String timeIn,String timeOut,String totalHours)
{
database=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(MyDatabaseHelper.Project,project);
values.put(MyDatabaseHelper.WorkDescription,workDescription);
values.put(MyDatabaseHelper.Per,per);
values.put(MyDatabaseHelper.TimeIn,timeIn);
values.put(MyDatabaseHelper.TimeOut,timeOut);
values.put(MyDatabaseHelper.TotalHours,totalHours);
database.insert(MyDatabaseHelper.TABLE_WORKDETAILS,null,values);
database.close();
return 0 ;
}
}
WorkDetails.java
public void add() {
button.setOnClickListener
.....
TI.insertTableInfo(name,weather,date,status);
WD.insertWorkDetails(a,b,c,d,e,f); // Do I need to include the foreign key?
}
答案 0 :(得分:0)
是的,你以正确的方式做到了