AlphaPieter1 AlphaPieter1 - 4 months ago 24
Android Question

Check if an value already exists in SQLite Database

I am trying to code an account system, the accounts are stored inside an SQLite database. Whenever someone wants to create an account I first want to check if there is already an account that uses the same email(the email is not the primary key but simple text).

public boolean checkemail(String email)
{
SQLiteDatabase db = this.getReadableDatabase();
String Query = "Select * from " + TABLE_KANIDAT + " where " + KEY_KEMAIL + " = " + "'"+email+"'";
Cursor cursor = db.rawQuery(Query, null);
if(cursor.getCount() <= 0)
{
cursor.close();
return false;
}
else
{
cursor.close();
return true;
}
}

Answer

create a method

public boolean checkAlreadyExist(String email)
    {
        String query = SELECT + YOUR_EMAIL_COLUMN + FROM + TABLE_NAME + WHERE + YOUR_EMAIL_COLUMN + " =?";
        Cursor cursor = db.rawQuery(query, new String[]{email});
        if (cursor.getCount() > 0)
        {
            return false;
        }
        else
            return true;
    }
Comments