How can I check whether the column already has value? Can use cursor to find?
I have two tables, one is table info
and another is table workDetails
Table Info: ID(PK), Name, Weather, Date, Status, TimeIn_Info, TimeOut_Info
Table WorkDetails: ID(PK),Project,WorkDescription,Per,TimeIn,TimeOut // 4 row
There are 4 row in my WorkDetails and I only want the row which has value only will be inserted. And for the TimeOut_Info
, it will get from TimeOut
which is in table WorkDetails
. If row 4 is empty, it will goes for row 3. If there exits a TimeOut
value in row 3, it will insert into table Info and will not look for row 2 and row 1....
public void checkRow2(String a, String b, String c, String d) {
if ((TextUtils.isEmpty(a)) && (TextUtils.isEmpty(b)) && (TextUtils.isEmpty(c)) && (TextUtils.isEmpty(d))) {
// Toast.makeText(getApplicationContext(), "Doss", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Row 2 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))) {
WD.insertWorkDetails(a2, 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);
Toast.makeText(getApplicationContext(), "No Per2", Toast.LENGTH_LONG).show();
}
The coding else-if
path I attached doesn't work although it already meet the condition. I wonder is it the way to check whether the selected column already has a value?
Cursor cur = databaseb.rawQuery("SELECT COlumn COUNT(*) FROM +MyDatabaseHelper.TABLE_INFO+ ", null);
if (cur.getCount() > 0){
//do nothing everything's as it should be
}
else{
//
}
I have tried another method, but still no luck :(
A bit difficult to figure out what you are trying to do, but if you for example have a User
table, keyed by UserId
, with an optional (nullable) UserDescription
, and you want to check if a particular user has a description, the SQL to do so can be one of the following:
-- Return non-null value if user has description, null if not, or no record if user not found
SELECT UserDescription
FROM User
WHERE UserId = ?
-- Return 1 if user has description, 0 if not, or no record if user not found
SELECT CASE WHEN UserDescription IS NULL THEN 0 ELSE 1 END AS HasDescription
FROM User
WHERE UserId = ?
-- Return 1 if user has description, or no record if not or if user not found
SELECT 1
FROM User
WHERE UserId = ?
AND UserDescription IS NOT NULL
-- Return 1 if user has description, or 0 if not or if user not found
SELECT COUNT(*)
FROM User
WHERE UserId = ?
AND UserDescription IS NOT NULL
There are more ways to do it.