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();
}
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();
}
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();
}
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;
}
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);
}
}
}
Aggregations