检查SQLite数据库中是否已存在值

时间:2016-07-20 11:12:42

标签: android sqlite android-sqlite

我正在尝试对帐户系统进行编码,帐户存储在SQLite数据库中。每当有人想要创建帐户时,我首先要检查是否已有使用相同电子邮件的帐户(该电子邮件不是主要密钥而是简单文本)。

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;
    }
}

2 个答案:

答案 0 :(得分:3)

创建方法

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;
    }

答案 1 :(得分:1)

Try this

         public boolean rowIdExists(String StrId) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("select id from " + TABLE_USERRATE
            + " where id=?", new String[]{StrId});
    boolean exists = (cursor.getCount() > 0);
    /*cursor.close();
    db.close();*/
    return exists;
}


    if (rowIdExists("ValuesId")) {
            //do something
            } else {
              //do something  
            }