Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only allow unique data entry with Android SQLite?

Before I get into describing by problem I'd like to point out I am aware of the other threads asking this question, however none for me have been able to solve my issue.

I've been working on a sharing app using the BumpAPI, which upon receiving the chunk, saves it to an SQLite database for retrieval in a list view activity, this is all working fine and the data is saved, however if the same text is sent twice it will be saved again and again and the list view will show this, from what I've read I need the 'UNIQUE' identifier? however being completely new to SQL I am at a loss with regards to achieving this, here is my DataHelper class which im using to create and add the entries, would anyone be kind enough to modify it or inform me of a possible solution?

Thanks very much

    public class DataHelper {

   private static final String DATABASE_NAME = "tags.db";
   private static final int DATABASE_VERSION = 1;
   private static final String TABLE_NAME = "TagTable";

   private Context context;
   private SQLiteDatabase db;

   private SQLiteStatement insertStmt;
   private static final String INSERT = "insert into "
      + TABLE_NAME + "(name) values (?)";

   public DataHelper(Context context) {
      this.context = context;
      OpenHelper openHelper = new OpenHelper(this.context);
      this.db = openHelper.getWritableDatabase();
      this.insertStmt = this.db.compileStatement(INSERT);
   }

   public long insert(String name) {
      this.insertStmt.bindString(1, name);
      return this.insertStmt.executeInsert();

   }



   public void deleteAll() {
      this.db.delete(TABLE_NAME, null, null);
   }

   public List<String> selectAll() {
      List<String> list = new ArrayList<String>();
      Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" }, 
        null, null, null, null, "name desc");
      if (cursor.moveToFirst()) {
         do {
            list.add(cursor.getString(0)); 
         } while (cursor.moveToNext());
      }
      if (cursor != null && !cursor.isClosed()) {
         cursor.close();
      }
      return list;
   }

   private static class OpenHelper extends SQLiteOpenHelper {

      OpenHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase db) {
         db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name    TEXT)" + "text unique, " + "ON CONFLICT REPLACE");
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         onCreate(db);
      }
   }
}
like image 774
Broak Avatar asked Nov 27 '11 14:11

Broak


People also ask

How to set UNIQUE constraint in SQLite?

Introduction to SQLite UNIQUE constraint To define a UNIQUE constraint, you use the UNIQUE keyword followed by one or more columns. You can define a UNIQUE constraint at the column or the table level. Only at the table level, you can define a UNIQUE constraint across multiple columns.

How to use DISTINCT in SQLite Android?

Here first argument specifies whether to use distinct or not. Pass only category in columns[] , as you just need that column in result as shown in your row query, and you will get distinct categories.

What is SQLiteDatabase in Android?

SQLite Database is an open-source database provided in Android which is used to store data inside the user's device in the form of a Text file. We can perform so many operations on this data such as adding new data, updating, reading, and deleting this data.


1 Answers

Add the unique keyword to the column.

db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT unique);
like image 121
Broak Avatar answered Oct 01 '22 03:10

Broak