Beispiel #1
0
  @SuppressLint("LongLogTag")
  @Override
  public Uri insert(Uri uri, ContentValues values) {

    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    final int match = sUriMatcher.match(uri);

    Uri returnUri;

    if (match == USERS) {

      try {
        long _id = db.insert(UserContract.UserEntry.TABLE_NAME, null, values);
        returnUri = UserContract.UserEntry.buildUserUri(_id);
      } catch (SQLiteConstraintException e) {
        Log.e("Username unique exception: ", e.toString());
        returnUri = UserContract.UserEntry.buildUserUri(-1);
      }

    } else {
      throw new UnsupportedOperationException("Unknown uri: " + uri);
    }

    if (db != null) db.close();

    getContext().getContentResolver().notifyChange(uri, null);
    return returnUri;
  }
Beispiel #2
0
  /* Insert new request into table 'request' */
  void insertRequest(int number) {

    // Log.i("PRIME", "Table : request : Inside insertRequest() : No -> "
    //		 										+ number);

    SQLiteDatabase db = this.getWritableDatabase();

    // ??
    String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

    // Log.i("PRIME", "DbHandler : insertRequest() "
    //		 				+ "howmany -> " + number);

    ContentValues values = new ContentValues();
    values.put("howmany", number);
    values.put("current_count", 0);
    values.put("is_complete", "N");
    values.put("created_time", timeStamp);
    values.put("tstamp", timeStamp);

    try {
      // Inserting Row
      db.insertOrThrow(TABLE_REQUEST, null, values);

    } catch (android.database.sqlite.SQLiteConstraintException e) {
      Log.e("PRIME", "insertRequest : SQLiteConstraintException:" + e.getMessage());
    } catch (android.database.sqlite.SQLiteException e) {
      Log.e("PRIME", "SQLiteException:" + e.getMessage());
    } catch (Exception e) {
      Log.e("PRIME", "Exception:" + e.getMessage());
    }

    db.close(); // Closing database connection
  }
Beispiel #3
0
  @Override
  public Uri insert(final Uri uri, final ContentValues values) {
    final String tableName;
    final Uri contentUri;
    switch (uriMatcher.match(uri)) {
      case UM_ITEMS:
        tableName = Item.TABLE_NAME;
        contentUri = Item.CONTENT_URI;
        break;
      case UM_ITEMTAGS:
        tableName = ItemTag.TABLE_NAME;
        contentUri = ItemTag.CONTENT_URI;
        break;
      case UM_SUBSCRIPTIONS:
        tableName = Subscription.TABLE_NAME;
        contentUri = Subscription.CONTENT_URI;
        break;
      case UM_SUBSCRIPTIONTAGS:
        tableName = SubscriptionTag.TABLE_NAME;
        contentUri = SubscriptionTag.CONTENT_URI;
        break;
      case UM_SETTINGS:
        tableName = Setting.TABLE_NAME;
        contentUri = Setting.CONTENT_URI;
        break;
      case UM_TAGS:
        tableName = Tag.TABLE_NAME;
        contentUri = Tag.CONTENT_URI;
        break;
      case UM_TRANSACTIONS:
        tableName = Transaction.TABLE_NAME;
        contentUri = Transaction.CONTENT_URI;
        break;
      default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    final SQLiteDatabase database = mDbHelper.getWritableDatabase();
    try {
      final long rowId = database.insertOrThrow(tableName, tableName, values);
      if (rowId > 0) {
        final Uri insertedUri = ContentUris.withAppendedId(contentUri, rowId);
        return insertedUri;
      }
    } catch (final SQLiteConstraintException exception) {
      exception.printStackTrace();
    }
    return null;
  }
Beispiel #4
0
  private void handleShareSenz(Senz senz) {
    // SwitchesDB db = new SwitchesDB(context);
    DBSource db = new DBSource(context);
    // if senz already exists in the db, SQLiteConstraintException should throw
    // get gpio and save in database
    // Log.e(TAG,"============="+senz.getAttributes().size());
    try {
      db.deleteTable();
      Log.e(TAG, senz.getAttributes() + "=======================================");

      for (Map.Entry<String, String> entry : senz.getAttributes().entrySet()) {
        String key = entry.getKey();
        if (!key.contains("homez") && !key.contains("time")) {
          db.createSwitch(key);
        }
        if (key.contains("homez")) {
          Log.e(
              TAG,
              "=================App =  "
                  + key
                  + "  User "
                  + senz.getSender().getUsername()
                  + " added to DB======================");
          db.deleteUser(senz.getSender().getUsername());
          db.createUser(senz.getSender().getUsername());
          db.resetUserStatus();
          db.setUserStatus(senz.getSender().getUsername(), 1);
        }
      }

      NotificationUtils.showNotification(
          context,
          context.getString(R.string.new_senz),
          "SmartHome Switches are Shared from @" + senz.getSender().getUsername());
      Log.e(TAG, "Swithes and User are Added To Homes DB");
    } catch (SQLiteConstraintException e) {
      Log.e(TAG, e.toString());
    }
  }
Beispiel #5
0
  public void insertPrimeNo(int nthNo, int primeNo) {

    // Log.v("PRIME", "Table : prime : Inside insertPrimeNo()");

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("nthno", nthNo);
    values.put("primeno", primeNo);
    try {
      // Inserting Row
      db.insertOrThrow(TABLE_PRIME, null, values);

    } catch (android.database.sqlite.SQLiteConstraintException e) {
      Log.e("PRIME", "insertPrimeNo() : SQLiteConstraintException:" + e.getMessage());
    } catch (android.database.sqlite.SQLiteException e) {
      Log.e("PRIME", "insertPrimeNo() : SQLiteException:" + e.getMessage());
    } catch (Exception e) {
      Log.e("PRIME", "insertPrimeNo() : Exception:" + e.getMessage());
    }
    db.close(); // Closing database connection
  }
  @Override
  public int bulkInsert(Uri aUri, ContentValues[] aValues) {
    String table = null;
    int result = 0;

    SQLiteDatabase db = mDbHelper.getWritableDatabase();

    final int match = sUriMatcher.match(aUri);
    switch (match) {
      case FORUM:
        table = TABLE_FORUM;
        break;
      case SUBFORUM:
        table = TABLE_SUBFORUM;
        break;
    }

    db.beginTransaction();

    try {
      for (ContentValues value : aValues) {
        db.insert(table, "", value);
        result++;
      }

      db.setTransactionSuccessful();

      if (result > 0) {
        getContext().getContentResolver().notifyChange(aUri, null);
      }
    } catch (SQLiteConstraintException e) {
      Log.i(TAG, e.toString());
    } finally {
      db.endTransaction();
    }

    return result;
  }
Beispiel #7
0
  /**
   * Saves the given task to the database.getDatabase(). Task must already exist. Returns true on
   * success.
   *
   * @param task
   * @return true if save occurred, false otherwise (i.e. nothing changed)
   */
  public boolean save(Task task) {
    boolean saveSuccessful = false;
    if (task.getId() == Task.NO_ID) {
      try {
        saveSuccessful = createNew(task);
      } catch (SQLiteConstraintException e) {
        if (e.getMessage().contains(Task.REMOTE_ID_PROPERTY_NAME)) {
          // Tried to create task with remote id that already exists
          saveSuccessful = false;
          TodorooCursor<Task> cursor =
              query(Query.select(Task.ID).where(Task.REMOTE_ID.eq(task.getValue(Task.REMOTE_ID))));
          if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            task.setId(cursor.get(Task.ID));
            saveSuccessful = saveExisting(task);
          }
        }
      }
    } else {
      saveSuccessful = saveExisting(task);
    }

    return saveSuccessful;
  }