Skip to content Skip to sidebar Skip to footer

Getting Null Value In Foreign Key Column

I have two table (Information and WorkDetails) in SQLite where WorkDetails has a foreign key which refer to Information. When everytime the data inserted, the foreign key should fo

Solution 1:

change your insert method like this so you get your table_info id

publiclonginsertTimeSheet(String name)
{
    database=dbHelper.getWritableDatabase();
    ContentValues values=newContentValues();
    values.put(MyDatabaseHelper.Name,name);
    database.insert(MyDatabaseHelper.TABLE_INFO,null,values);
    Cursorcursor= database.rawQuery("SELECT MAX(ID) FROM Table_Info", null);
    database.close();
    return cursor.getLong(0) ;
}

and change your code

longid = ts.insertTimeSheet(name); // refer to TimeSheetAPI
WD.insertWorkDetails(a1,W1,P1,b,c,th,id);

your insertwd should

public long insertWorkDetails(String project, String workDescription, String per,String timeIn,String timeOut,String totalHours, long id)
{
    database=dbHelper.getWritableDatabase();
    ContentValues values=newContentValues();
    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);
    values.put("TableInfo_id", id);
    database.insert(MyDatabaseHelper.TABLE_WORKDETAILS,null,values);
    database.close();
    return0 ;

}

Solution 2:

You need to pass the inserted parent row id (TABLE_INFO ID) to your "insertWorkDetails" method after you actually inserted the parent row in "insertTimeSheet" method.

public long insertWorkDetails(Integer id, String project, String workDescription, String per,String timeIn,String timeOut,String totalHours){ ... }

Foreign keys won't get updated by themselves.

Post a Comment for "Getting Null Value In Foreign Key Column"