5

I'm trying to create a simple favorites application, where you can keep your favorites pages (from the web for example).

Let's say I have the following data in my database

Title    | URL     | TAG      |
-------------------------------
Hey       Ho.com    Site
Jo        Jo.com    Image
Jo        Mo.com    Image

Now I want to make sure, the user does not add the same page twice, so I will check if the value already exists. But I need to that the user does not add a site to the table that has already been added.

So let's say I would try to add:

Title    | URL     | TAG      |
-------------------------------
Jo        Mo.com    Image

This it would return true (for "check if row exist", because an identical row already exists).

But if I would try to add:

Title    | URL     | TAG      |
-------------------------------
Jo        Go.com    Image

It would return false (though the title already exists), because there is no identical row.

This is the code where I add my data to the database with:

public long createNote(String title, String url, String rn) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_URL, url);
    initialValues.put(KEY_RN, rn);

    return mDb.insert(DATABASE_TABLE, null, initialValues);

How can I check if a row already exists in a database?

1
  • 1
    Use INTEGER PRIMARY KEY AUTOINCREMENT ID. Always use INTEGER PRIMARY KEY AUTOINCREMENT ID. To prevent duplicate rows you just need a UNIQUE constraint. See this related answer stackoverflow.com/a/2701903/792932 Commented Dec 19, 2013 at 16:56

3 Answers 3

7

You can check using cursor:

public boolean checkEvent(String title, String URL, String tag) 
{
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLES, 
            new String[] { KEY_TITLE,KEY_URL,KEY_TAG }, 
            KEY_TITLE + " = ? and "+ KEY_URL + " = ? and " + KEY_TAG + " = ?" , 
            new String[] {title,url,tag}, 
            null, null, null, null);

    if(cursor.moveToFirst())

     return true; //row exists
    else 
     return false;

}

Edit: Code now copy & paste ready

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

Comments

5

There is a very graceful (imho) way to do this at the database level. Add this to your create table sql when you create your database:

 UNIQUE(TITLE, URL) ON CONFLICT REPLACE 

Then you can insert all you want without checking, it will either be added or updated if the row is already there.

2 Comments

Creating a unique identifier by combining various data is certainly an Idea!
This is a far better and superior solution than the one which has been accepted. Definitely deserves an up vote. Thank :)
1

You can query to see if the a unique identifier is there in the database, in your case the title maybe the unique identifier of the row.

public boolean isEntry(int title){
    String queryString = "SELECT * FROM "+TABLE_NAME+" WHERE "+KEY_PROJECT_ID+" = "+"'"+project_id+"'";
    Cursor c = db.rawQuery(queryString, null);
    if(c.getCount() > 0){
        Log.i("CHECK", "true");
        return true;
    }
    else{ 
        return false;
    }
}

Here true would mean that there is a row like this, false would mean that there is no such entry.

Now let's say are about to insert an entry into the database. Even if the method returns a true value what you can do is you could compare the other two values as in your example. After that you could call a function to return a single row from the database, like

public String getCursor(int project_id){
    String queryString = "SELECT * FROM "+TABLE_NAME+" WHERE "+KEY_PROJECT_ID+" = "+"'"+project_id+"'";
    Cursor cursor = db.rawQuery(queryString, null);
    return cursor;
}

Once you have the function you can select the TAG and URL from it and see if it matches the one in your database.

Like this:

Cursor cursor = db.rawQuery(queryString, null);
String tag = "";
String url = "";

cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
    like_state = cursor.getString(cursor.getColumnIndex(KEY_TAG));
    url = cursor.getString(cursor.getColumnIndex(KEY_URL));
    cursor.moveToNext();
}
cursor.close();

if(!ins_tag.equalsIgnoreCase(tag) || !ins_url.equalsIgnoreCase(url)) {
    //insert function here
}   

And as mentioned in the comments you should use the an AUTOINCREAMENT integer as a Primary Key.

2 Comments

Thank you for your answer, but I am really looking for a way to find identical rows
Thank you, I'll try and let you know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.