Advertisement
Ankhwatcher

ContentProvider Insert

Nov 15th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1.  @Override
  2.     public Uri insert(Uri uri, ContentValues contentValues) {
  3.  
  4.         if (mBookOpenHelper == null)
  5.             mBookOpenHelper = new BookOpenHelper(this.getContext());
  6.  
  7.         SQLiteDatabase sqLiteDatabase = mBookOpenHelper.getWritableDatabase();
  8.         try {
  9.             long newID = sqLiteDatabase.insertOrThrow(BookOpenHelper.BOOK_TABLE, null, contentValues);
  10.             if (newID > 0) {
  11.                 Uri newUri = ContentUris.withAppendedId(uri, newID);
  12.                 getContext().getContentResolver().notifyChange(uri, null);
  13.                 sqLiteDatabase.close();
  14.                 return newUri;
  15.             } else {
  16.                 throw new SQLException("Failed to insert row into " + uri);
  17.  
  18.             }
  19.         } catch (SQLiteConstraintException e) {
  20.             Log.i(this.getClass().getName(), "Ignoring constraint failure.");
  21.         } catch (SQLException e) {
  22.             e.printStackTrace();
  23.         }
  24.         return null;
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement