Search code examples
androidsqlitecursor

getcount returning 0


Hey guys this is my code to compare a name with something existing in the database already. However the getcount returns 0 all the time. Any help ? thanks !!

public String checkUserName(SQLiteDatabase db, String enteredName)

{

        String selectionArgs[] = new String[1];
        selectionArgs[0] = enteredName;
        Cursor cursor = db.rawQuery("select * from table1 where user=?", selectionArgs);
        cursor.moveToFirst();
        return Integer.toString(cursor.getCount());
    }

The code i use to call the above function is :

    Intent intent = getIntent();
    TextView tv1 = new TextView(this);
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE)
tv1.setText(db.checkUserName(db.getWritableDatabase(), message));
relativeLayout.addView(tv1)

DB snapshot link : https://i.sstatic.net/LnTbl.jpg


Solution

  • Found what was wrong..

    modified code as :

    Cursor cursor = db.rawQuery("select * from table1 where user like ?", selectionArgs);
    

    with LIKE in sqlite, 'string%' is used to compare two column entries in addition to the above method