good day everyone. I'm new to android and now using cursor to check whether the specific column is null or not. If is null, the value will be inserted into selected column. The code work fine actually, but they are inserted into two different row. How can I solve that so that value c and d will insert into same row but different column?
public void checkRow1(String a, String b, String c, String d)
{
WD.insertWorkDetails(a1, a, b, c, d);
Cursor mCursor = database.rawQuery("SELECT TimeOut_Info FROM " + MyDatabaseHelper.TABLE_INFO + " WHERE " +
MyDatabaseHelper.TimeOut_Info + "= '" + d + "'", null);
if (mCursor == null) {
database=dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MyDatabaseHelper.TimeOut_Info, d);
database.insert(MyDatabaseHelper.TABLE_INFO, null, values);
return ;
}
database=dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MyDatabaseHelper.TimeIn_Info, c);
database.insert(MyDatabaseHelper.TABLE_INFO, null, values);
Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_LONG).show();
return;
}
Edited
In order to check whether there already has a value in a specific column with a specific row, I use **COUNT**
public void checkRow3(String a, String b, String c, String d) {
if ((TextUtils.isEmpty(a)) && (TextUtils.isEmpty(b)) && (TextUtils.isEmpty(c)) && (TextUtils.isEmpty(d))) {
// Toast.makeText(getApplicationContext(), "Do", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Row 3 is empty", Toast.LENGTH_LONG).show();
return;
} else if ((a != null && a.trim().length() > 0) && ((TextUtils.isEmpty(b)) && (c != null && c.trim().length() > 0) && (d != null && d.trim().length() > 0))) {
// Toast.makeText(getApplicationContext(), "Done1", Toast.LENGTH_LONG).show();
WD.insertWorkDetails(a3, a, b, c, d);
Cursor mCursor = database.rawQuery("SELECT COUNT(TimeOut_Info) FROM " + MyDatabaseHelper.TABLE_INFO ,null);
if (mCursor.getCount()>0) {
Toast.makeText(getApplicationContext(), "Already has value", Toast.LENGTH_LONG).show();
return;
}
else
{
database = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MyDatabaseHelper.TimeOut_Info, d);
database.insert(MyDatabaseHelper.TABLE_INFO, null, values);
Toast.makeText(getApplicationContext(), "Inserted Time Out", Toast.LENGTH_LONG).show();
}
} else if ((a != null && a.trim().length() > 0) && (TextUtils.isEmpty(b)) && (TextUtils.isEmpty(c)) && (TextUtils.isEmpty(d))) {
WD.insertWorkDetails(a3, a, b, c, d);
// Toast.makeText(getApplicationContext(), "Row 3 is empty", Toast.LENGTH_LONG).show();
}
But I get "Already has a value" even the specific column is null. wHY WOULD THIS HAPPEN?