Search in sources :

Example 1 with SQLiteConstraintException

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

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 2 with SQLiteConstraintException

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

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 3 with SQLiteConstraintException

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

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 4 with SQLiteConstraintException

use of android.database.sqlite.SQLiteConstraintException in project Remindy by abicelis.

the class RemindyDAO method getTask.

/**
     * Returns a Task (with Reminder and Attachments) given a taskId
     * @param taskId    The ID of the Task to get.
     */
public Task getTask(int taskId) throws CouldNotGetDataException, SQLiteConstraintException {
    SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
    Cursor cursor = db.query(RemindyContract.TaskTable.TABLE_NAME, null, RemindyContract.PlaceTable._ID + "=?", new String[] { String.valueOf(taskId) }, null, null, null);
    if (cursor.getCount() == 0)
        throw new CouldNotGetDataException("Specified Task not found in the database. Passed id=" + taskId);
    if (cursor.getCount() > 1)
        throw new SQLiteConstraintException("Database UNIQUE constraint failure, more than one record found. Passed value=" + taskId);
    cursor.moveToNext();
    Task task = getTaskFromCursor(cursor);
    task.setAttachments(getAttachmentsOfTask(taskId));
    if (task.getReminderType() != ReminderType.NONE)
        task.setReminder(getReminderOfTask(taskId, task.getReminderType()));
    return task;
}
Also used : CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) Task(ve.com.abicelis.remindy.model.Task) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Cursor(android.database.Cursor)

Example 5 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)

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