Marc Ortiz
Marc Ortiz

Reputation: 2402

Android Sqlite query detect when an element doesn't exists in the table

How can I detect when an element doesn't exist in my table? I need because I want to update/insert contacts on it. My problem it's that I want to insert a new contact by using ContentObserver but this element is called multiple times and I'm selecting the last element. So when i insert a new contact, I select the last element, I'm trying to identify if exists on the db and insert it.

Upvotes: 1

Views: 807

Answers (2)

Andy
Andy

Reputation: 123

You can check by using count(*) function in database.

Upvotes: 0

Siva K
Siva K

Reputation: 4968

use a boolean value to check whether the contact exist or not

boolean contact = myDbHelper.checkidExitsorNot(ur table name,row name , value);

public boolean checkidExitsorNot(String tablename, String rowname, String id) {
    String queryf = "select * from " + tablename + " where " + rowname  + "='" + Integer.valueOf(id) + "'";

    Cursor c = myDataBase.rawQuery(queryf, null);
    if (c.getCount() == 0) {
        c.close();
        return true;
    }else {
        c.close();
        return false;
    }
}

if the return is true then it does not exist if false it exist

Upvotes: 2

Related Questions