title: How to check if a value already exists in the database in Android
body: <p>I've a database that I will populate with items after I parse a JSON response. How can I check if the values are already present in the database and prevent inserting again ?</p>
  
  <p>Eg. Name of Database: Exicom.sqlite</p>
  
  <p>Name of table: TimeReport</p>
  
  <p>Fields in Database: userID, clientName</p>
  
  <p>I am using SQLite in android.</p>
  
  <p>Thanks for your input</p>
  
  <p>This is my HELPER class. I've skipped opening and closing the database as it will be too long. Followed up by where I will call the method to insert values into the database.</p>
  
  <pre><code>public static final String indexNo = "index_no";
  public static final String user_id = "userId";
  public static final String company_id = "companyId";
  public static final String user_name = "username";
  public static final String client_Id = "clientId";
  public static final String project_Id = "projectId";
  public static final String report_Id = "reportId";
  public static final String niv_1  = "niv1";
  public static final String niv_2 = "niv2";
  public static final String work_type_id = "workTypeId";
  public static final String time_type_id = "timeTypeId";
  public static final String date_id = "dateId";
  public static final String month_id = "monthId";
  public static final String year_id = "yearId";
  public static final String hourS = "hours";
  public static final String private_comment = "privateComment";
  public static final String Ncomment = "comment";
  public static final String mod_flag = "modFlag";
  public static final String new_flag = "newFlag";
  public static final String open_flag = "openFlag";
  public static final String delete_flag = "deleteFlag";
  
  private static final String DATABASE_NAME = "CopernicusDB.sqlite";
  private static final String DATABASE_TABLE = "TimeReportTable";
  private static final int    DATABASE_VERSION = 1;
  
  private final Context context;
  private static DatabaseHelper DBHelper;
  private static SQLiteDatabase db;
  private static String TAG = "##---SecondActivityUserHelper---##";
  public SecondActivityUserHelper(Context ctx)
  {
  this.context = ctx;
  DBHelper = new DatabaseHelper(context);
  }
  
  private static class DatabaseHelper extends SQLiteOpenHelper{
  
      public DatabaseHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
          Log.v(TAG,"The DatabaseHelper method ");
      }
  
      public long insertIntoDatabase(String companyId,String username, String dateId,
              String clientId,String projectId,String niv1,String niv2,String workTypeId,String timeTypeId,
              String hours,String comment,String privateComment,String openFlag,String reportId) 
              {
              ContentValues initialValues = new ContentValues();
              initialValues.put(company_id, companyId);
              initialValues.put(user_name, username);
              initialValues.put(date_id,dateId);
              initialValues.put(client_Id,clientId);
              initialValues.put(project_Id,projectId);
              initialValues.put(niv_1,niv1);
              initialValues.put(niv_2,niv2);
              initialValues.put(work_type_id,workTypeId);
              initialValues.put(time_type_id,timeTypeId);
              initialValues.put(hourS, hours);
              initialValues.put(Ncomment, comment);
              initialValues.put(private_comment,privateComment);
              initialValues.put(open_flag, openFlag);
              initialValues.put(report_Id,reportId);
              Log.v(TAG, "Inserted into database sucessfully");
              return db.insert(DATABASE_TABLE, null, initialValues);
              }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>useradapter.openDatabase();
  long id = dB.insertIntoDatabase(  newcompanyid,newusername,newdate,
  newClientId,newprojectId,newniv1,newniv2,newworktypeid,newtimetypeid,
  newhours,newcomment,newprivatecomment,newopen,newreportid);
                               useradapter.closeDatabase();
  </code></pre>