1

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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Y you did not accept this answer? as before you done now you changed?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.