0
votes

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?

2
Anyone can help? Thanks - Hoo
Look at using the update method to update (database.update()) using your primary key to identify the row to be updated rather than using the insert method. - enigman
If I want to check whether the first row in boh column is null or not, can I use if (mCursor == null) ? - Hoo

2 Answers

0
votes

rawQuery() always returns a cursor. If no data was found, this cursor will be empty, i.e., moveToFirst() will return false.

A simple query with SELECT COUNT(...) FROM ... will always return a single row, containing a single value, so the return value of the cursor's getCount() will always be 1.

Please note that COUNT() counts rows; to check if a column is NULL, you have to use a WHERE clause.

To get the count value, you have to actually read it from the cursor (with moveToFirst() and getInt(0)), or use a helper function that does this for you:

long timeInfos = DatabaseUtils.longForQuery(database,
         "SELECT COUNT(*) FROM " + MyDatabaseHelper.TABLE_INFO +
         " WHERE TimeOut_Info IS NOT NULL", null);
if (timeInfos > 0) {
    ...
0
votes

To check whether column has null value use method isNull.