Obtaining the NULL value in the foreign key column

advertisements

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 follow the number of PK in Information. However, I get NULL value in foreign key column. The PK in Information and WorkDetails Table is auto-increment.

MyDatabaseAdapter.java

  public void onCreate(SQLiteDatabase db)
        {
           db.execSQL("create table "+TABLE_INFO+"(ID INTEGER PRIMARY KEY ,Name 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)");
        }

WorkDetailsTable.java

WD= new com.example.project.project.API.WorkDetailsAPI(this);
 ts= new com.example.project.project.API.InfoAPI(this);

    Button btn1=(Button)findViewById(R.id.button2);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                W1=txtWork1.getText().toString();
                W2=txtWork2.getText().toString();
                W3=txtWork3.getText().toString();
                W4=txtWork4.getText().toString();
                a1 = spinnerTra.getSelectedItem().toString();
                a2= spinnerTra2.getSelectedItem().toString();
                a3 = spinnerTra3.getSelectedItem().toString();
                a4=spinnerTra4.getSelectedItem().toString();
                P1=per1.getText().toString();
                P2=per2.getText().toString();
                P3=per3.getText().toString();
                P4=per4.getText().toString();
                ts.insertTimeSheet(name); // refer to TimeSheetAPI
                WD.insertWorkDetails(a1,W1,P1,b,c,th); // insert multiple row and refer to WorkDetailsAPI
                WD.insertWorkDetails(a2,W2,P2,d,e1,th);
                WD.insertWorkDetails(a3, W3, P3, f, g,th);
                WD.insertWorkDetails(a4,W4,P4,h,i,th);
            }
        });

InfoAPI.java

   public class InfoAPI {
    private SQLiteDatabase database;
    private MyDatabaseHelper dbHelper;
    public String[] allColumns={MyDatabaseHelper.ID,MyDatabaseHelper.Name};

    public InfoAPI(Context context)
    {
        dbHelper=new MyDatabaseHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();

    }

    public void close() {
        dbHelper.close();
    }
    public long insertTimeSheet(String name)
    {
        database=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(MyDatabaseHelper.Name,name);
        database.insert(MyDatabaseHelper.TABLE_INFO,null,values);
        database.close();
        return 0 ;

    }

}

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)
    {
        dbHelper=new MyDatabaseHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }
    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 ;

    }

}


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

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

and change your code

long id = 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=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);
    values.put("TableInfo_id", id);
    database.insert(MyDatabaseHelper.TABLE_WORKDETAILS,null,values);
    database.close();
    return 0 ;

}