0

I'm new to Android SQLite. I've created the table name "Registration":

id FirstName LastName UserName Password Mobile
0  Rakesh     L        rocky   pw123    9600956892
1  Ramesh     S        ram     wckt123  9600634845
2  Vignesh    A        vicky   vky123   9380930489
3  Balaji     B        bala    ball123  9597735613

If I register again with the same username and password, it must show the Toast. I've tried the following snippet .But i failed to succeed.

  DBHelper.getReadableDatabase();
            Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});
            if (mCursor != null) {
                if(mCursor.getCount()!=0)
                {
                    return true;
                }
            }
         return false;
}

Please help me with code snippets.

Rakesh L
  • 132
  • 3
  • 10
  • db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username='"+ username +"' AND password='"+ password+"'", null); – moDev Feb 26 '13 at 06:16

2 Answers2

1

Try this code...

You just check Cursor not null after that why you check count not 0.

So, that you try this...

DBHelper.getReadableDatabase();

Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE    username=? AND password=?", new String[]{username,password});

if (mCursor != null)
{
                return true;
    /* record exist */
}
else
{
            return false;
    /* record not exist */
}
Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
  • Well, count not 0 is actually necessary. On not existing rows the cursor is not null, so this is not enough. – User Nov 22 '13 at 16:10
  • Although I'm using query instead of rawQuery, maybe that makes a difference. – User Nov 22 '13 at 16:11
0

Check using Cursor as below:

Cursor c = <YourDatabase>.rawQuery("SELECT * FROM " + DB_TABLE+ " WHERE " + FIELD + "= '" + VALUE + "'");
if(c == null)
{
  //doesn't exists therefore insert record.
}
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90