Ejemplo n.º 1
0
  public void remove(Dictionary dictionary, List<Vocable> vocables) {
    SQLiteDatabase db = getWritableDatabase();

    db.beginTransaction();
    try {
      if (dictionary.getId() != -1) {
        db.delete(
            VOCABLE_TABLE_NAME,
            DICTIONARY_ID_COL_NAME + " = ?",
            new String[] {"" + dictionary.getId()});
        db.delete(
            DICTIONARY_TABLE_NAME, ID_COL_NAME + " = ?", new String[] {"" + dictionary.getId()});
      }

      db.setTransactionSuccessful();
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage(), e);
    } finally {
      db.endTransaction();
    }
  }
Ejemplo n.º 2
0
  public void persist(Dictionary dictionary, List<Vocable> vocables) {
    SQLiteDatabase db = getWritableDatabase();

    db.beginTransaction();
    try {
      int dictionaryId = dictionary.getId();
      if (dictionaryId != -1) {
        ContentValues values = new ContentValues();
        values.put(PICTURE_COL_NAME, dictionary.getPicture());
        values.put(NAME_COL_NAME, dictionary.getName());
        int ret =
            db.update(
                DICTIONARY_TABLE_NAME,
                values,
                ID_COL_NAME + " = ?",
                new String[] {"" + dictionaryId});
        if (ret < 1) {
          // if dictionary was deleted meanwhile
          dictionaryId = -1;
        }
      }
      if (dictionaryId == -1) {
        dictionaryId = getDictionaryIdNext(db);
        addDictionary(db, dictionaryId, dictionary.getPicture(), dictionary.getName());
      }

      db.delete(
          VOCABLE_TABLE_NAME, DICTIONARY_ID_COL_NAME + " = ?", new String[] {"" + dictionaryId});

      int vocableId = getVocableIdNext(db);
      for (Vocable vocable : vocables) {
        addVocable(db, vocableId, dictionaryId, vocable.getPicture(), vocable.getWord());
        vocableId++;
      }

      db.setTransactionSuccessful();
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage(), e);
    } finally {
      db.endTransaction();
    }
  }