1

I created a database (sqlite) for bookmarks/favorites (button). The table has only 2 rows (id, bookmark). Everytime I click the bookmark button, it keeps on adding the same bookmark (string) in the bookmark column.

Here's my database code (DatabaseHelper.java) :

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Bookmark.db";
public static final String TABLE_NAME = "bookmark_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "BOOKMARK";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY   AUTOINCREMENT, BOOKMARK TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i2) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String bookmark){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, bookmark);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
    return res;
}
}

How can I make "bookmark" column unique?

1 Answer 1

1

Just add UNIQUE after your column description, like this:

db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY  AUTOINCREMENT, BOOKMARK TEXT UNIQUE)");
Sign up to request clarification or add additional context in comments.

Comments

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.