title: Android app freezing on SQLite db insert
body: <p>I'm running into a really strange issue trying to insert into a local database. I am adding a record to 2 different tables and the first time I add the value it shows up in the list until the activity is destroyed. I can navigate away and come back and the new value is there but when I close the app and clear it from the history it is gone.</p>
  
  <p>The second time I try and enter a value into the database the UI freezes and it doesn't seem to throw any exceptions or give me any ideas of what is going on.</p>
  
  <p>Here's some code.</p>
  
  <pre><code>public void addVehicleService(VehicleService service)
  {
  
      SQLiteDatabase dbWritable = getWritableDatabase();
      int serviceId;
      if (serviceExists(service))
      {
          addServicePerformed(service, null, dbWritable);
      }
      else
      {
          ContentValues values = new ContentValues();
          values.put(VEHICLE_ID, service.getVehicleId());
          values.put(SERVICE_NAME, service.getServiceName());
          values.put(SERVICE_INTERVAL, service.getInterval());
          values.put(SERVICE_MILEAGE, service.getOdometer());
          values.put(DAYS_TO_NEXT_NOTIFICATION, 0);
          values.put(NOTIFIED, 0);
          values.put(INITIAL_NOTIFICATION_DATE, Utility.FormatDate(Calendar.getInstance()));
  
          serviceId = (int) dbWritable.insert(VEHICLE_SERVICE_TABLE, null, values);
          if (serviceId != -1)
              addServicePerformed(service, serviceId, dbWritable);
          else
              Log.d("DBTAG", "Error inserting new Service");
      }
  }
  
  
  private boolean serviceExists(VehicleService service)
  {
      SQLiteDatabase db = getReadableDatabase();
  
      Cursor cursor = db.query(VEHICLE_SERVICE_TABLE, new String[]{ID}, VEHICLE_ID + " = ? AND " + SERVICE_NAME + " = ? AND " + SERVICE_INTERVAL + " = ?",
              new String[]{service.getVehicleId().toString(), service.getServiceName(), service.getInterval().toString()}, null, null, null);
      int count = cursor.getCount();
      cursor.close();
  
      return count &gt; 0;
  }
  
  private void addServicePerformed(VehicleService service, @Nullable Integer serviceId, SQLiteDatabase dbWritable)
  {
  
      if (serviceId == null)
      {
          dbWritable.beginTransaction();
          serviceId = getServiceId(service, dbWritable);
      }
  
      ContentValues values = new ContentValues();
      values.put(SERVICE_ID, serviceId);
      values.put(ODOMETER_AT_SERVICE, service.getOdometer());
      values.put(DATE_OF_SERVICE, Utility.FormatDate(service.getDate()));
      values.put(COST_OF_SERVICE, service.getCost());
  
      dbWritable.insert(SERVICE_PERFORMED_TABLE, null, values);
  
  }
  </code></pre>
  
  <p>I do have some methods that insert into the database and the changes are retained, so I'm really confused as to what's going on.</p>