Java Code Examples for android.database.sqlite.SQLiteDatabase.insertOrThrow()

The following are Jave code examples for showing how to use insertOrThrow() of the android.database.sqlite.SQLiteDatabase class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: MyFlightbookAndroid   File: DBCache.java   View Source Code Vote up 6 votes
public void updateCache(String tablename) {
    this.errorString = "";
    SQLiteDatabase db = MFBMain.mDBHelper.getWritableDatabase();

    try {
        deleteTableEntry(db, tablename);
        ContentValues values = new ContentValues();
        Date dt = new Date();
        SimpleDateFormat s = new SimpleDateFormat(MFBConstants.TIMESTAMP, Locale.US);

        values.put(COL_LASTREFRESH, s.format(dt));
        values.put(COL_TABLENAME, tablename);

        db.insertOrThrow(CACHE_TABLE, null, values);
    } catch (Exception ex) {
        this.errorString = "Error updating cache for table " + tablename + ex.getMessage();
        Log.e(MFBConstants.LOG_TAG, this.errorString);
    }
}
 
Example 2
Project: Movie-Notifier-Android   File: DBHelper.java   View Source Code Vote up 6 votes
public void addCinema(Cinema cinema) {
    if(getCinemaByID(cinema.getID()) != null) {
        updateCinema(cinema);
    } else {
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction();
        try {
            db.insertOrThrow("Cinemas", null, cinema.toContentValues());
            db.setTransactionSuccessful();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
        }
    }
}
 
Example 3
Project: inventum   File: MovieProvider.java   View Source Code Vote up 6 votes
@Nullable
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    MovieUriEnum matchingUriEnum = mUriMatcher.matchUri(uri);

    if (matchingUriEnum.table != null) {
        try {
            db.insertOrThrow(matchingUriEnum.table, null, values);
        } catch (SQLiteConstraintException e) {
            throw e;
        }
    }

    switch (matchingUriEnum) {
        case MOVIES:
            return Movies.buildMovieUri(values.getAsString(Movies.MOVIE_ID));
        case TVS:
            return TVs.buildTVUri(values.getAsString(TVs.TV_ID));
        case PERSONS:
            return Persons.buildPersonUri(values.getAsString(Persons.PERSON_ID));
        default:
            throw new UnsupportedOperationException("Unknown insert URI: " + uri);
    }
}
 
Example 4
Project: orgzly-android   File: Provider.java   View Source Code Vote up 6 votes
private int updateOrInsertBookSync(
        SQLiteDatabase db,
        long bookId,
        String repoUrl,
        String rookUrl,
        String revision,
        long mtime) {

    long repoUrlId = getOrInsertRepoUrl(db, repoUrl);
    long rookUrlId = DbRookUrl.getOrInsert(db, rookUrl);
    long rookId = getOrInsertRook(db, rookUrlId, repoUrlId);
    long rookRevisionId = getOrInsertVersionedRook(db, rookId, revision, mtime);

    ContentValues values = new ContentValues();
    values.put(DbBookSync.BOOK_VERSIONED_ROOK_ID, rookRevisionId);
    values.put(DbBookSync.BOOK_ID, bookId);

    long id = DatabaseUtils.getId(db, DbBookSync.TABLE, DbBookSync.BOOK_ID + "=" + bookId, null);
    if (id != 0) {
        db.update(DbBookSync.TABLE, values, DbBookSync._ID + "=" + id, null);
    } else {
        db.insertOrThrow(DbBookSync.TABLE, null, values);
    }

    return 1;
}
 
Example 5
Project: airgram   File: SettingManager.java   View Source Code Vote up 6 votes
private void setValue(String key, String value) {
    SQLiteDatabase writableDatabase = this.openHelper.getWritableDatabase();
    writableDatabase.beginTransaction();
    try {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBConstants.SETTING_COL_KEY, key);
        contentValues.put(DBConstants.SETTING_COL_VALUE, value);
        if (getValue(key) == null) {
            writableDatabase.insertOrThrow(DBConstants.TABLE_SETTING, null, contentValues);
        } else {
            writableDatabase.update(DBConstants.TABLE_SETTING, contentValues, "key='" + key + "'", null);
        }
        writableDatabase.setTransactionSuccessful();
    } finally {
        writableDatabase.endTransaction();
    }
}
 
Example 6
Project: orgzly-android   File: DbRookUrl.java   View Source Code Vote up 6 votes
public static long getOrInsert(SQLiteDatabase db, String rookUrl) {
    long id = DatabaseUtils.getId(
            db,
            TABLE,
            ROOK_URL + "=?",
            new String[] { rookUrl });

    if (id == 0) {
        ContentValues values = new ContentValues();
        values.put(ROOK_URL, rookUrl);

        id = db.insertOrThrow(TABLE, null, values);
    }

    return id;
}
 
Example 7
Project: Show_Chat   File: MovieProvider.java   View Source Code Vote up 6 votes
@Nullable
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    MovieUriEnum matchingUriEnum = mUriMatcher.matchUri(uri);

    if (matchingUriEnum.table != null) {
        try {
            db.insertOrThrow(matchingUriEnum.table, null, values);
        } catch (SQLiteConstraintException e) {
            throw e;
        }
    }

    switch (matchingUriEnum) {
        case MOVIES:
            return Movies.buildMovieUri(values.getAsString(Movies.MOVIE_ID));
        case TVS:
            return TVs.buildTVUri(values.getAsString(TVs.TV_ID));
        case PERSONS:
            return Persons.buildPersonUri(values.getAsString(Persons.PERSON_ID));
        default:
            throw new UnsupportedOperationException("Unknown insert URI: " + uri);
    }
}
 
Example 8
Project: orgzly-android   File: Provider.java   View Source Code Vote up 6 votes
/**
 * Gets {@link OrgDateTime} from database or inserts a new record if it doesn't exist.
 * @return {@link OrgDateTime} database ID
 */
private long getOrInsertOrgRange(SQLiteDatabase db, OrgRange range) {
    long id = DatabaseUtils.getId(
            db,
            DbOrgRange.TABLE,
            DbOrgRange.STRING + "=?",
            new String[] { range.toString() });

    if (id == 0) {
        ContentValues values = new ContentValues();

        long startTimestampId = getOrInsertOrgTime(db, range.getStartTime());

        long endTimestampId = 0;
        if (range.getEndTime() != null) {
            endTimestampId = getOrInsertOrgTime(db, range.getEndTime());
        }

        DbOrgRange.toContentValues(values, range, startTimestampId, endTimestampId);

        id = db.insertOrThrow(DbOrgRange.TABLE, null, values);
    }

    return id;
}
 
Example 9
Project: orgzly-android   File: DbNote.java   View Source Code Vote up 6 votes
/**
 * Gets {@link OrgDateTime} from database or inserts a new record if it doesn't exist.
 * @return {@link OrgDateTime} database ID
 */
private static long getOrInsertOrgRange(SQLiteDatabase db, OrgRange range) {
    long id = DatabaseUtils.getId(
            db,
            DbOrgRange.TABLE,
            DbOrgRange.STRING + "=?",
            new String[] { range.toString() });

    if (id == 0) {
        ContentValues values = new ContentValues();

        long startTimestampId = getOrInsertOrgTime(db, range.getStartTime());

        long endTimestampId = 0;
        if (range.getEndTime() != null) {
            endTimestampId = getOrInsertOrgTime(db, range.getEndTime());
        }

        DbOrgRange.toContentValues(values, range, startTimestampId, endTimestampId);

        id = db.insertOrThrow(DbOrgRange.TABLE, null, values);
    }

    return id;
}
 
Example 10
Project: orgzly-android   File: DbPropertyValue.java   View Source Code Vote up 6 votes
public static long getOrInsert(SQLiteDatabase db, String value) {
    long id = DatabaseUtils.getId(
            db,
            TABLE,
            VALUE + " = ?",
            new String[] { value });

    if (id == 0) {
        ContentValues values = new ContentValues();
        values.put(VALUE, value);

        id = db.insertOrThrow(TABLE, null, values);
    }

    return id;
}
 
Example 11
Project: encdroidMC   File: EDDBHelper.java   View Source Code Vote up 6 votes
public void addRule(FileSynchronizerRule rule){
	SQLiteDatabase db = getWritableDatabase();
	ContentValues values = new ContentValues();

	values.clear();
	//values.put("id", rule.getId());
	values.put("id", rule.getId());
	values.put("volumeNameToSync", rule.getVolumeNameToSync());
	values.put("volumePassword", rule.getVolumePassword());
	values.put("volumePathToSync", rule.getVolumePathToSync());
	values.put("deleteSrcFileAfterSync", rule.getDeleteSrcFileAfterSync());
	values.put("syncOnlyOnWifi", rule.getSyncOnlyOnWifi());
	values.put("localPathToSync", rule.getLocalPathToSync());

	Log.d(TAG, "addRule()");

	db.insertOrThrow("SYNC_RULES", null, values);
	
}
 
Example 12
Project: orgzly-android   File: Provider.java   View Source Code Vote up 6 votes
private long getOrInsertRepoUrl(SQLiteDatabase db, String repoUrl) {
    long id = DatabaseUtils.getId(
            db,
            DbRepo.TABLE,
            DbRepo.REPO_URL + "=?",
            new String[] { repoUrl });

    if (id == 0) {
        ContentValues values = new ContentValues();
        values.put(DbRepo.REPO_URL, repoUrl);
        values.put(DbRepo.IS_REPO_ACTIVE, 0);

        id = db.insertOrThrow(DbRepo.TABLE, null, values);
    }

    return id;
}
 
Example 13
Project: orgzly-android   File: Provider.java   View Source Code Vote up 6 votes
private int updateOrInsertBookLink(SQLiteDatabase db, long bookId, String repoUrl, String rookUrl) {
    long repoUrlId = getOrInsertRepoUrl(db, repoUrl);
    long rookUrlId = DbRookUrl.getOrInsert(db, rookUrl);
    long rookId = getOrInsertRook(db, rookUrlId, repoUrlId);

    ContentValues values = new ContentValues();
    values.put(DbBookLink.BOOK_ID, bookId);
    values.put(DbBookLink.ROOK_ID, rookId);

    long id = DatabaseUtils.getId(db, DbBookLink.TABLE, DbBookLink.BOOK_ID + "=" + bookId, null);
    if (id != 0) {
        db.update(DbBookLink.TABLE, values, DbBookLink._ID + "=" + id, null);
    } else {
        db.insertOrThrow(DbBookLink.TABLE, null, values);
    }

    return 1;
}
 
Example 14
Project: app_secompufscar   File: DatabaseHandler.java   View Source Code Vote up 5 votes
public void addAtividade(Atividade atividade) throws SQLiteException {
    SQLiteDatabase db = this.getWritableDatabase();
    db.insertOrThrow(TABLE_ATIVIDADE, null, getAtividadeRow(atividade));
    db.close(); // Closing database connection

    addMinistrantes(atividade);
}
 
Example 15
Project: InAppTranslation_Android   File: InAppTranslation.java   View Source Code Vote up 5 votes
protected void cacheTransaltion(Context context, String key, String value, String lang){
    // Create new helper
    IATDatabaseHelper dbHelper = new IATDatabaseHelper(context);
    // Get the database. If it does not exist, this is where it will
    // also be created.
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    // Create insert entries
    ContentValues values = new ContentValues();
    values.put(IATTranslationContract.Translation.COLUMN_KEY, key);
    values.put(IATTranslationContract.Translation.COLUMN_VALUE, value);
    values.put(IATTranslationContract.Translation.COLUMN_LANG, lang);

    // Insert the new row, returning the primary key value of the new row
    try{
        long newRowId;
        newRowId = db.insertOrThrow(
                TABLE_NAME,
                null,
                values);
    }catch(SQLiteConstraintException sce){
        Log.d(TAG, "key(" + key + ") already exist for lang(" + lang + ")");
        //android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: Translation.key, Translation.lang (code 2067)
    }catch(Exception e) {
        e.printStackTrace();
    }
}
 
Example 16
Project: orgzly-android   File: Provider.java   View Source Code Vote up 5 votes
private long insertRootNote(SQLiteDatabase db, long bookId) {
    Note rootNote = Note.newRootNote(bookId);

    ContentValues values = new ContentValues();
    NotesClient.toContentValues(values, rootNote);
    replaceTimestampRangeStringsWithIds(db, values);

    return db.insertOrThrow(DbNote.TABLE, null, values);
}
 
Example 17
Project: app_secompufscar   File: DatabaseHandler.java   View Source Code Vote up 4 votes
public void addPatrocinador(Patrocinador patrocinador) throws SQLiteConstraintException {
    SQLiteDatabase db = this.getWritableDatabase();
    db.insertOrThrow(TABLE_PATROCINADOR, null, getPatrocinadorRow(patrocinador));
    db.close(); // Closing database connection
}
 
Example 18
Project: app_secompufscar   File: DatabaseHandler.java   View Source Code Vote up 4 votes
public void addPessoa(Pessoa pessoa) throws SQLiteConstraintException {
    SQLiteDatabase db = this.getWritableDatabase();
    db.insertOrThrow(TABLE_PESSOA, null, getPessoaRow(pessoa));
    db.close(); // Closing database connection
}
 
Example 19
Project: android-tdd-persistence   File: TaskDBStorage.java   View Source Code Vote up 3 votes
@Override
public void insert(TaskDBModel taskDBModel) {
    SQLiteDatabase db = openDB();

    ContentValues values = toContentValues(taskDBModel);

    db.insertOrThrow(TABLE_NAME, null, values);

    closeDB();
}
 
Example 20
Project: app_secompufscar   File: DatabaseHandler.java   View Source Code Vote up 3 votes
/**
 * Operações para a tabela Ministrante
 **/

public void addMinistrantes(Atividade atividade) {

    if (atividade.getMinistrantes() != null) {

        List<Pessoa> pessoas = atividade.getMinistrantes();

        addManyPessoas(pessoas);

        ContentValues values = new ContentValues();
        values.put("ID_ATIVIDADE", atividade.getId());

        SQLiteDatabase db = this.getWritableDatabase();

        for (int i = 0; i < pessoas.size(); i++) {
            values.put("ID_PESSOA", pessoas.get(i).getId());

            try {
                db.insertOrThrow(TABLE_MINISTRANTE, null, values);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        db.close(); // Closing database connection
    }
}