title: Sqlite database insert query issue inside 'try catch' block
body: <p>I am developing an app(requires person's location) that stores location coordinates against the consumerIds, I have created three fields,namely 1)key_Con_ID, 2) key_Latitude, 3)key_Longitude.</p>
  
  <p>The insert code is inside a 'try' and 'catch'. The 'try' part returns true after successful insert whereas the 'catch' part returns false.</p>
  
  <p>The problem I'm facing is,while debugging, the 'try' and 'catch', both the parts of the code are executed. And also when I viewed the table(created a backup of database on SD card), there were duplicate entries only of some consumers.
  <strong>My question is,if the insert query is successful,why is the code in the 'catch' part being executed?</strong></p>
  
  <pre><code>public class sqldataclass{
  public static final String key_Con_ID="Con_ID";
  public static final String key_Latitude="Latitude";
  public static final String key_Longitude = "Longitude";
  
  private static final String DATABASE_TABLE11 = "Gps_Data";
  private static final int DATABASE_VERSION = 1;
  private static final String DEBUG_TAG = null;
  
  private DbHelper OurHelper;
  private final Context OurContext;
  private SQLiteDatabase OurDatabase;
  private static class DbHelper extends SQLiteOpenHelper {
  
      public DbHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          String CREATE_GPS_DATA = "CREATE TABLE " + DATABASE_TABLE11 + " ( "
                  + key_Con_ID + " varchar ,"
                  + key_Latitude + " double ,"
                  + key_Longitude + " double )";
          try
            {db.execSQL(CREATE_GPS_DATA);}
            catch(SQLException e){e.printStackTrace();}
  }
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  
          db.execSQL("DROP TABLE IF EXIST" + DATABASE_NAME);
          onCreate(db);
      }
      public sqldataclass(Context c) {
      OurContext = c;
  }
  
  public sqldataclass open() throws SQLException
  
  {
      OurHelper = new DbHelper(OurContext);
      OurDatabase = OurHelper.getWritableDatabase();
      return this;
  }
      public void close() {
      OurHelper.close();
  }
  public boolean insertGpsCords(String consid,double lat,double longi)
  {
      open();
      try {
          ContentValues values = new ContentValues();
  
          values.put(sqldataclass.key_Con_ID, consid);
          values.put(sqldataclass.key_Latitude, lat);
          values.put(sqldataclass.key_Longitude, longi);
  
          String query1 = "Select " + key_Con_ID + " from " + DATABASE_TABLE11 + " where " + key_Con_ID + " = '" + consid + "'";
  
          Cursor c1 = OurDatabase.rawQuery(query1, null);
          int getRecordCount = c1.getCount();
          if (getRecordCount == 0) {
              OurDatabase.insert(DATABASE_TABLE11, null, values);
          } else {
                  OurDatabase.update(DATABASE_TABLE11, values, null, null);
          }
          close();
          return true;
      }
      catch (Exception e) {
          return false;
      }
  }
  </code></pre>
  
  <p>The insertGpsCords function is invoked on click of a button from an activity.The location is obtained via a service.
  The part of the code is here:-</p>
  
  <pre><code>b1= (Button) findViewById(R.id.b1);
  b1.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
  if (s!=null){
  
                  ArrayList&lt;Double&gt; loc = new ArrayList&lt;Double&gt;();
  
                  if (s.getCoordinates().size()!=0) {
                      loc.clear();
                      loc.addAll(s.getCoordinates());
  
                      Double l = loc.get(0);
                      Double g = loc.get(1);
                      Toast.makeText
                              (Bill_meter_status_faulty.this, "latitude: " + l + "longitude: " + g, Toast.LENGTH_SHORT)
                              .show();
                      sqldataclass sdc = new sqldataclass(Bill_meter_status_faulty.this);
                      sdc.insertGpsCords(consId, l, g);
                  }
                  else{
                      Toast.makeText(Bill_meter_status_faulty.this,"cannot access location",Toast.LENGTH_SHORT).show();
                  }
  
              }
  </code></pre>
  
  <p>I have no issue obtaining the location.</p>