Search in sources :

Example 6 with SQLiteConstraintException

use of android.database.sqlite.SQLiteConstraintException in project iosched by google.

the class ScheduleProvider method insert.

/** {@inheritDoc} */
@Override
public Uri insert(Uri uri, ContentValues values) {
    LOGV(TAG, "insert(uri=" + uri + ", values=" + values.toString() + ", account=" + getCurrentAccountName(uri, false) + ")");
    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    ScheduleUriEnum matchingUriEnum = mUriMatcher.matchUri(uri);
    if (matchingUriEnum.table != null) {
        try {
            db.insertOrThrow(matchingUriEnum.table, null, values);
            notifyChange(uri);
        } catch (SQLiteConstraintException exception) {
            // bootstrap file issue.
            throw exception;
        }
    }
    switch(matchingUriEnum) {
        case BLOCKS:
            {
                return Blocks.buildBlockUri(values.getAsString(Blocks.BLOCK_ID));
            }
        case CARDS:
            {
                return ScheduleContract.Cards.buildCardUri(values.getAsString(ScheduleContract.Cards.CARD_ID));
            }
        case TAGS:
            {
                return Tags.buildTagUri(values.getAsString(Tags.TAG_ID));
            }
        case ROOMS:
            {
                return Rooms.buildRoomUri(values.getAsString(Rooms.ROOM_ID));
            }
        case SESSIONS:
            {
                return Sessions.buildSessionUri(values.getAsString(Sessions.SESSION_ID));
            }
        case SESSIONS_ID_SPEAKERS:
            {
                return Speakers.buildSpeakerUri(values.getAsString(SessionsSpeakers.SPEAKER_ID));
            }
        case SESSIONS_ID_TAGS:
            {
                return Tags.buildTagUri(values.getAsString(Tags.TAG_ID));
            }
        case MY_SCHEDULE:
            {
                values.put(MySchedule.MY_SCHEDULE_ACCOUNT_NAME, getCurrentAccountName(uri, false));
                db.insertOrThrow(Tables.MY_SCHEDULE, null, values);
                notifyChange(uri);
                Uri sessionUri = Sessions.buildSessionUri(values.getAsString(MyScheduleColumns.SESSION_ID));
                notifyChange(sessionUri);
                return sessionUri;
            }
        case MY_VIEWED_VIDEOS:
            {
                values.put(MyViewedVideos.MY_VIEWED_VIDEOS_ACCOUNT_NAME, getCurrentAccountName(uri, false));
                db.insertOrThrow(Tables.MY_VIEWED_VIDEO, null, values);
                notifyChange(uri);
                Uri videoUri = Videos.buildVideoUri(values.getAsString(MyViewedVideos.VIDEO_ID));
                notifyChange(videoUri);
                return videoUri;
            }
        case MY_FEEDBACK_SUBMITTED:
            {
                values.put(MyFeedbackSubmitted.MY_FEEDBACK_SUBMITTED_ACCOUNT_NAME, getCurrentAccountName(uri, false));
                db.insertOrThrow(Tables.MY_FEEDBACK_SUBMITTED, null, values);
                notifyChange(uri);
                Uri sessionUri = Sessions.buildSessionUri(values.getAsString(MyFeedbackSubmitted.SESSION_ID));
                notifyChange(sessionUri);
                return sessionUri;
            }
        case SPEAKERS:
            {
                return Speakers.buildSpeakerUri(values.getAsString(Speakers.SPEAKER_ID));
            }
        case ANNOUNCEMENTS:
            {
                return Announcements.buildAnnouncementUri(values.getAsString(Announcements.ANNOUNCEMENT_ID));
            }
        case SEARCH_SUGGEST:
            {
                return SearchSuggest.CONTENT_URI;
            }
        case MAPMARKERS:
            {
                return MapMarkers.buildMarkerUri(values.getAsString(MapMarkers.MARKER_ID));
            }
        case MAPTILES:
            {
                return MapTiles.buildFloorUri(values.getAsString(MapTiles.TILE_FLOOR));
            }
        case FEEDBACK_FOR_SESSION:
            {
                return Feedback.buildFeedbackUri(values.getAsString(Feedback.SESSION_ID));
            }
        case HASHTAGS:
            {
                return Hashtags.buildHashtagUri(values.getAsString(Hashtags.HASHTAG_NAME));
            }
        case VIDEOS:
            {
                return Videos.buildVideoUri(values.getAsString(Videos.VIDEO_ID));
            }
        default:
            {
                throw new UnsupportedOperationException("Unknown insert uri: " + uri);
            }
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Uri(android.net.Uri)

Example 7 with SQLiteConstraintException

use of android.database.sqlite.SQLiteConstraintException in project Meltdown by phubbard.

the class ItemProvider method applyBatch.

/**
     * Apply the given set of {@link ContentProviderOperation}, executing inside
     * a {@link SQLiteDatabase} transaction. All changes will be rolled back if
     * any single one fails.
     * 
     * @note this is from the Google iosched (Google IO 2012) code.
     */
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    Long tzero = System.currentTimeMillis();
    Log.i(TAG, "Bulk ops called");
    if (helper == null)
        helper = new dbHelper(getContext(), DB_VERSION);
    final SQLiteDatabase db = helper.getWritableDatabase();
    db.beginTransaction();
    try {
        final int numOperations = operations.size();
        final ContentProviderResult[] results = new ContentProviderResult[numOperations];
        for (int i = 0; i < numOperations; i++) {
            try {
                results[i] = operations.get(i).apply(this, results, i);
            } catch (SQLiteConstraintException sqe) {
            //Log.d(TAG, "ignoring constraint");
            }
        }
        db.setTransactionSuccessful();
        Long deltat = System.currentTimeMillis() - tzero;
        Double ops_per_sec = operations.size() / ((float) deltat / 1000.0);
        Log.i(TAG, String.format("%d msec to do %d ops, %7.2f ops per second", deltat, operations.size(), ops_per_sec));
        return results;
    } finally {
        db.endTransaction();
    }
}
Also used : ContentProviderResult(android.content.ContentProviderResult) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException)

Example 8 with SQLiteConstraintException

use of android.database.sqlite.SQLiteConstraintException in project orWall by EthACKdotOrg.

the class NatRules method update.

public boolean update(AppRule appRule) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(natDBHelper.COLUMN_APPNAME, appRule.getPkgName());
    contentValues.put(natDBHelper.COLUMN_APPUID, String.valueOf(appRule.getAppUID()));
    contentValues.put(natDBHelper.COLUMN_ONIONTYPE, appRule.getOnionType());
    contentValues.put(natDBHelper.COLUMN_LOCALHOST, appRule.getLocalHost() ? 1 : 0);
    contentValues.put(natDBHelper.COLUMN_LOCALNETWORK, appRule.getLocalNetwork() ? 1 : 0);
    String filter = natDBHelper.COLUMN_APPUID + "=?";
    String[] filterArgs = { String.valueOf(appRule.getAppUID()) };
    SQLiteDatabase db = this.dbHelper.getWritableDatabase();
    int nb_row = 0;
    try {
        nb_row = db.update(natDBHelper.NAT_TABLE_NAME, contentValues, filter, filterArgs);
    } catch (SQLiteConstraintException e) {
        Log.e(TAG, "Constraint exception");
        Log.e(TAG, e.getMessage());
    }
    db.close();
    return (nb_row == 1);
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException)

Example 9 with SQLiteConstraintException

use of android.database.sqlite.SQLiteConstraintException in project android_frameworks_base by ParanoidAndroid.

the class DatabaseStatementTest method testStatementConstraint.

@MediumTest
public void testStatementConstraint() throws Exception {
    mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL);");
    SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
    // Try to insert NULL, which violates the constraint
    try {
        statement.clearBindings();
        statement.execute();
        fail("expected exception not thrown");
    } catch (SQLiteConstraintException e) {
    // expected
    }
    // Make sure the statement can still be used
    statement.bindLong(1, 1);
    statement.execute();
    statement.close();
    Cursor c = mDatabase.query("test", null, null, null, null, null, null);
    int numCol = c.getColumnIndexOrThrow("num");
    c.moveToFirst();
    long num = c.getLong(numCol);
    assertEquals(1, num);
    c.close();
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Cursor(android.database.Cursor) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 10 with SQLiteConstraintException

use of android.database.sqlite.SQLiteConstraintException in project platform_frameworks_base by android.

the class DatabaseStatementTest method testStatementConstraint.

@MediumTest
public void testStatementConstraint() throws Exception {
    mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL);");
    SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
    // Try to insert NULL, which violates the constraint
    try {
        statement.clearBindings();
        statement.execute();
        fail("expected exception not thrown");
    } catch (SQLiteConstraintException e) {
    // expected
    }
    // Make sure the statement can still be used
    statement.bindLong(1, 1);
    statement.execute();
    statement.close();
    Cursor c = mDatabase.query("test", null, null, null, null, null, null);
    int numCol = c.getColumnIndexOrThrow("num");
    c.moveToFirst();
    long num = c.getLong(numCol);
    assertEquals(1, num);
    c.close();
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Cursor(android.database.Cursor) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Aggregations

SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)15 Cursor (android.database.Cursor)10 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)8 SQLiteStatement (android.database.sqlite.SQLiteStatement)7 MediumTest (android.test.suitebuilder.annotation.MediumTest)6 CouldNotGetDataException (ve.com.abicelis.remindy.exception.CouldNotGetDataException)3 ContentValues (android.content.ContentValues)2 PlaceNotFoundException (ve.com.abicelis.remindy.exception.PlaceNotFoundException)2 Task (ve.com.abicelis.remindy.model.Task)2 OneTimeReminder (ve.com.abicelis.remindy.model.reminder.OneTimeReminder)2 RepeatingReminder (ve.com.abicelis.remindy.model.reminder.RepeatingReminder)2 ContentProviderResult (android.content.ContentProviderResult)1 SQLException (android.database.SQLException)1 Uri (android.net.Uri)1 ServerIdInfo (com.vodafone360.people.database.DatabaseHelper.ServerIdInfo)1 Calendar (java.util.Calendar)1 Time (ve.com.abicelis.remindy.model.Time)1 LocationBasedReminder (ve.com.abicelis.remindy.model.reminder.LocationBasedReminder)1 Reminder (ve.com.abicelis.remindy.model.reminder.Reminder)1 TaskTriggerViewModel (ve.com.abicelis.remindy.viewmodel.TaskTriggerViewModel)1