title: Android transaction or trigger?
body: <p>I am working on a fuel use application which will run on Android 1.6 onwards. The bundled SQLite on v1.6 doesn't do foreign keys, so I've had to handle it manually. So far, I have done this using an Android transaction:</p>
  
  <pre><code>    public static long addFuelUp(String registrationNumber, String date)
  {
      SQLiteDatabase db = uKMpgData.getReadableDatabase();
      long result = -1;
      ContentValues values = new ContentValues();
      Cursor vehicleCursor = VehicleDataProvider.getVehicle(registrationNumber);
  
      if(vehicleCursor.moveToNext())
      {
          Cursor fuelUpsCursor = getFuelUps(registrationNumber, date);
          if(!fuelUpsCursor.moveToNext())
          {
              db.beginTransaction();
              try
              {
                      values.put(REGISTRATION_NO_COLUMN, registrationNumber.replace(" ", ""));
                      values.put(DATE_TIME_COLUMN, date);
                      result = db.insertOrThrow(FUEL_USE_TABLE_NAME, null, values);
                      db.setTransactionSuccessful();
              }
              catch(SQLException e)
              {
                  Log.d("addFuelUp", e.getMessage());
              }
              finally
              {
                  db.endTransaction();
                  vehicleCursor.close();
                  fuelUpsCursor.close();
              }
          }
      }
      return result;
  }
  </code></pre>
  
  <p>I.e. fuel data cannot be entered unless there is a matching vehicle registration number in the database.</p>
  
  <p>My question is, is there a better way to do this? I'm not a database expert, but I know you can set up triggers to enforce rules - are triggers more suited to handle constraints?</p>
  
  <p>Cheers,
  Barry</p>