How to insert foreign key into a table? I have two table (Info and WorkDetails) where WorkDetails has a foreign key which refer to Info. Is this the correct way to implement?
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?
}
Aucun commentaire:
Enregistrer un commentaire