示例#1
0
  @SuppressLint("SimpleDateFormat")
  @Override
  public Uri insert(Uri uri, ContentValues values) {
    // TODO Auto-generated method stub
    // 新增,故uri只能为collection
    if (sUriMatch.match(uri) != HISTORY_COLLECTION_URI_INDICATOR) {
      throw new IllegalArgumentException("URI =" + uri);
    }
    // 允许插入空行
    ContentValues cv;
    if (values == null) {
      cv = new ContentValues();
    } else {
      cv = new ContentValues(values);
    }
    //
    if (cv.containsKey(ChatHistoryTable.CHAT_TIME)) {
      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd-HH:mm:ss");
      String now = sdf.format(new Date(System.currentTimeMillis()));
      cv.put(ChatHistoryTable.CHAT_TIME, now);
    }

    mDb = mOpenHelper.getWritableDatabase();
    long rowId = mDb.insert(ChatHistoryTable.TABLE_NAME, null, cv);
    if (rowId > 0) {
      Uri insertUri = ContentUris.withAppendedId(ChatHistoryTable.CONTENT_URI, rowId);
      mContentResolver.notifyChange(insertUri, null);
      return insertUri;
    }
    throw new SQLiteException("Failed to insert!!!");
  }
示例#2
0
 private boolean initialize() {
   mContext = getContext();
   mContentResolver = mContext.getContentResolver();
   mOpenHelper = DatabaseHelper.getInstance(mContext);
   try {
     mDb = mOpenHelper.getWritableDatabase();
   } catch (SQLiteException ex) {
     mDb = mOpenHelper.getReadableDatabase();
   }
   return true;
 }