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
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.
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.
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