Example #1
0
  protected void runArchive() {

    SQLiteDatabase db = databaseHelper.getWritableDatabase();
    // TODO: add check to make sure this is not empty
    File dbFile = new File(db.getPath());
    db.close();
    if (archive.add(dbFile)) {
      dbFile.delete();
    }
    reloadDbHelper(manager);
    db = databaseHelper.getWritableDatabase(); // Build new database
    db.close();
  }
Example #2
0
  /** Deletes a row in the database */
  @Override
  public int delete(final Uri uri, final String where, final String[] whereArgs) {

    Helpers.validateSelection(where, sAppReadableColumnsSet);

    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int count;
    int match = sURIMatcher.match(uri);
    switch (match) {
      case MY_DOWNLOADS:
      case MY_DOWNLOADS_ID:
      case ALL_DOWNLOADS:
      case ALL_DOWNLOADS_ID:
        SqlSelection selection = getWhereClause(uri, where, whereArgs, match);
        deleteRequestHeaders(db, selection.getSelection(), selection.getParameters());
        count = db.delete(DB_TABLE, selection.getSelection(), selection.getParameters());
        break;

      default:
        Log.d(Constants.TAG, "deleting unknown/invalid URI: " + uri);
        throw new UnsupportedOperationException("Cannot delete URI: " + uri);
    }
    notifyContentChanged(uri, match);
    return count;
  }
Example #3
0
 public void trimHistory() {
   SQLiteOpenHelper helper = new DBHelper(activity);
   SQLiteDatabase db = null;
   Cursor cursor = null;
   try {
     db = helper.getWritableDatabase();
     cursor =
         db.query(
             DBHelper.TABLE_NAME,
             ID_COL_PROJECTION,
             null,
             null,
             null,
             null,
             DBHelper.TIMESTAMP_COL + " DESC");
     cursor.move(MAX_ITEMS);
     while (cursor.moveToNext()) {
       String id = cursor.getString(0);
       Log.i(TAG, "Deleting scan history ID " + id);
       db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + id, null);
     }
   } catch (SQLiteException sqle) {
     // We're seeing an error here when called in CaptureActivity.onCreate() in rare cases
     // and don't understand it. First theory is that it's transient so can be safely ignored.
     Log.w(TAG, sqle);
     // continue
   } finally {
     close(cursor, db);
   }
 }
  @Override
  public Uri insert(Uri uri, ContentValues values) {
    Uri result = null;
    boolean applyingBatch = applyingBatch();
    if (!applyingBatch) {
      mDb = mOpenHelper.getWritableDatabase();
      mDb.beginTransactionWithListener(this);
      try {
        result = insertInTransaction(uri, values);
        if (result != null) {
          mNotifyChange = true;
        }
        mDb.setTransactionSuccessful();
      } finally {
        mDb.endTransaction();
      }

      onEndTransaction();
    } else {
      result = insertInTransaction(uri, values);
      if (result != null) {
        mNotifyChange = true;
      }
    }
    return result;
  }
 @Override
 public Uri insert(Uri uri, ContentValues values) {
   Log.i(TAG, "insert " + TAG);
   SQLiteDatabase db = mDbHelper.getWritableDatabase();
   Long id = db.insert(Contacto.TABLE_NAME, null, values);
   return Uri.parse(Contacto.URI + "/" + id.toString());
 }
  @Override
  public int bulkInsert(Uri uri, ContentValues[] values) {
    // Resolve the URI.
    int match = mUriMatcher.match(uri);
    Mapping mapping = match != UriMatcher.NO_MATCH ? mMappings.get(match) : null;
    if (mapping == null || mapping.hasId) {
      throw new IllegalArgumentException("Invalid URI: " + uri);
    }

    // Insert all rows into the database and notify observers.
    String tableName = mapping.table.getTableName();
    SQLiteDatabase database = mDatabase.getWritableDatabase();
    int numInserted = 0;
    try {
      int length = values.length;
      database.beginTransaction();
      for (int i = 0; i != length; ++i) {
        database.insert(tableName, NULL_COLUMN_HACK, values[i]);
      }
      database.setTransactionSuccessful();
      numInserted = length;
    } finally {
      database.endTransaction();
    }
    notifyChange(uri);
    return numInserted;
  }
  @Override
  public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
    int count = 0;
    SQLiteDatabase db = openHelper.getWritableDatabase();

    int match = uriMatcher.match(uri);
    switch (match) {
      case CHATS:
        count = db.update(TABLE_CHAT, values, where, whereArgs);
        break;
      case MESSAGES:
        count = db.update(TABLE_MESSAGE, values, where, whereArgs);
        break;
      case CHAT_ID:
        count =
            db.update(
                TABLE_CHAT,
                values,
                ChatData.KEY_ID + "=" + Integer.parseInt(uri.getPathSegments().get(1)),
                null);
        break;
      case MESSAGE_ID:
        count =
            db.update(
                TABLE_MESSAGE,
                values,
                MessageData.KEY_ID + "=" + Integer.parseInt(uri.getPathSegments().get(1)),
                null);
        break;
      default:
        throw new UnsupportedOperationException("Cannot update URI " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
  }
  @Override
  public int bulkInsert(Uri uri, ContentValues[] values) {
    String table = uri.getLastPathSegment();
    SQLiteDatabase db = mSqLiteOpenHelper.getWritableDatabase();
    int res = 0;
    db.beginTransaction();
    try {
      for (ContentValues v : values) {
        long id = db.insert(table, null, v);
        db.yieldIfContendedSafely();
        if (id != -1) {
          res++;
        }
      }
      db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
    }
    String notify;
    if (res != 0
        && ((notify = uri.getQueryParameter(QUERY_NOTIFY)) == null || "true".equals(notify))) {
      getContext().getContentResolver().notifyChange(uri, null);
    }

    return res;
  }
 @Override
 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
     throws OperationApplicationException {
   HashSet<Uri> urisToNotify = new HashSet<Uri>(operations.size());
   for (ContentProviderOperation operation : operations) {
     urisToNotify.add(operation.getUri());
   }
   SQLiteDatabase db = mSqLiteOpenHelper.getWritableDatabase();
   db.beginTransaction();
   try {
     int numOperations = operations.size();
     ContentProviderResult[] results = new ContentProviderResult[numOperations];
     int i = 0;
     for (ContentProviderOperation operation : operations) {
       results[i] = operation.apply(this, results, i);
       if (operation.isYieldAllowed()) {
         db.yieldIfContendedSafely();
       }
       i++;
     }
     db.setTransactionSuccessful();
     for (Uri uri : urisToNotify) {
       getContext().getContentResolver().notifyChange(uri, null);
     }
     return results;
   } finally {
     db.endTransaction();
   }
 }
 public Uri insert(Uri paramUri, ContentValues paramContentValues) {
   SQLiteDatabase localSQLiteDatabase = mOpenHelper.getWritableDatabase();
   int i = paramUri.getPathSegments().size();
   if (i < 1) {
     throw new IllegalArgumentException("Unknown Uri");
   }
   long l2 = -1L;
   String str = (String) paramUri.getPathSegments().get(0);
   Object localObject = null;
   paramUri = (Uri) localObject;
   long l1 = l2;
   if (str.equals("suggestions")) {
     paramUri = (Uri) localObject;
     l1 = l2;
     if (i == 1) {
       l2 = localSQLiteDatabase.insert("suggestions", "query", paramContentValues);
       paramUri = (Uri) localObject;
       l1 = l2;
       if (l2 > 0L) {
         paramUri = Uri.withAppendedPath(mSuggestionsUri, String.valueOf(l2));
         l1 = l2;
       }
     }
   }
   if (l1 < 0L) {
     throw new IllegalArgumentException("Unknown Uri");
   }
   getContext().getContentResolver().notifyChange(paramUri, null);
   return paramUri;
 }
Example #11
0
  public void addHistoryItem(Result result, ResultHandler handler) {
    // Do not save this item to the history if the preference is turned off, or the contents are
    // considered secure.
    if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true)
        || handler.areContentsSecure()
        || !enableHistory) {
      return;
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
    if (!prefs.getBoolean(PreferencesActivity.KEY_REMEMBER_DUPLICATES, false)) {
      deletePrevious(result.getText());
    }

    ContentValues values = new ContentValues();
    values.put(DBHelper.TEXT_COL, result.getText());
    values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
    values.put(DBHelper.DISPLAY_COL, handler.getDisplayContents().toString());
    values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());

    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    try {
      db = helper.getWritableDatabase();
      // Insert the new entry into the DB.
      db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
    } finally {
      close(null, db);
    }
  }
  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {
    int count = 0;
    boolean applyingBatch = applyingBatch();
    if (!applyingBatch) {
      mDb = mOpenHelper.getWritableDatabase();
      mDb.beginTransactionWithListener(this);
      try {
        count = deleteInTransaction(uri, selection, selectionArgs);
        if (count > 0) {
          mNotifyChange = true;
        }
        mDb.setTransactionSuccessful();
      } finally {
        mDb.endTransaction();
      }

      onEndTransaction();
    } else {
      count = deleteInTransaction(uri, selection, selectionArgs);
      if (count > 0) {
        mNotifyChange = true;
      }
    }
    return count;
  }
  public static void doDbExecs(@NotNull SQLiteOpenHelper dbHelper, @NotNull List<DbExec> execs) {

    // assuming there is only one dbHelper per database in application
    synchronized (dbHelper) {
      SQLiteDatabase db = null;
      boolean wasOpened = false;
      try {
        db = dbCache.get(dbHelper);
        if (db == null) {
          db = dbHelper.getWritableDatabase();
          dbCache.put(dbHelper, db);
          wasOpened = true;
        }

        doDbTransaction(db, execs);

      } finally {
        // if database was opened - close it
        if (db != null && wasOpened) {
          // db.close();
          dbCache.remove(dbHelper);
        }
      }
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see android.content.ContentProvider#insert(android.net.Uri,
   * android.content.ContentValues)
   */
  @Override
  public Uri insert(Uri uri, ContentValues values) {

    ContentValues newValues = null;
    ContentValues outValues;
    Uri newUri = null;
    long rowId = 0;
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    if (db == null) {
      Log.v(TAG + " Fatal db object == null eeeek!");
      return null;
    }

    if (values != null) {
      outValues = new ContentValues(values);
    } else {
      outValues = new ContentValues();
    }

    switch (sUriMatcher.match(uri)) {
      case PROFILES:
        {
          newValues = preInsertProfiles(outValues);

          rowId = db.insert(PROFILES_TBL, null, newValues);
          if (rowId < 0) throw new SQLException("failed to insert profiles row " + uri);
          // update the insert row id
          // this is used for profile_id to link tables
          newUri = ContentUris.withAppendedId(Profile.Columns.CONTENT_URI, rowId);
          getContext().getContentResolver().notifyChange(newUri, null);
        }
        break;
      case CONTACTS:
        {
          rowId = db.insert(CONTACTS_TBL, null, outValues);
          if (rowId < 0) throw new SQLException("failed to insert contacts row " + uri);
          newUri = ContentUris.withAppendedId(Contacts.Columns.CONTENT_URI, rowId);
          getContext().getContentResolver().notifyChange(newUri, null);
          break; // <- that damn missing break took 10 minutes to find lol
        }

      case NOTIFICATION:
        {
          rowId = db.insert(NOTIFICATION_TBL, null, outValues);
          if (rowId < 0) throw new SQLException("failed to insert notification row " + uri);
          newUri = ContentUris.withAppendedId(Notify.Columns.CONTENT_URI, rowId);
          getContext().getContentResolver().notifyChange(newUri, null);
        }

        break;
      default:
        throw new IllegalArgumentException("Cannot insert null: " + uri);
    }

    if (Log.LOGV) Log.i(TAG + " new _id " + rowId + " new uri " + newUri);

    db.close();
    return newUri;
  }
Example #15
0
 public void clear() {
   String whereClause = "nid=?";
   String whereArgs[] = new String[] {String.valueOf(nid)};
   SQLiteDatabase db = helper.getWritableDatabase();
   db.delete("note", whereClause, whereArgs);
   db.close();
   notes.clear();
 }
Example #16
0
 public void remove(Note note) {
   String whereClause = "nid=? AND id=?";
   String whereArgs[] = new String[] {String.valueOf(nid), String.valueOf(note.getId())};
   SQLiteDatabase db = helper.getWritableDatabase();
   db.delete("note", whereClause, whereArgs);
   db.close();
   notes.remove(note);
 }
 // opens the database
 public POIDataHelper open() {
   db = helper.getWritableDatabase();
   this.insertMapStmt = db.compileStatement(INSERT_MAP);
   this.insertGroupStmt = db.compileStatement(INSERT_GROUP);
   this.insertPOIStmt = db.compileStatement(INSERT_POI);
   this.updatePOIStmt = db.compileStatement(UPDATE_POI);
   this.updatePOISelectedStmt = db.compileStatement(UPDATE_POI_SELECTED);
   return this;
 }
Example #18
0
  public static void addClient(SQLiteOpenHelper helper, Client client) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(ClientTable.NAME, client.getName());
    values.put(ClientTable.ACTIVE, client.isActive() ? "Y" : "N");
    values.put(ClientTable.DISCOUNT, client.getDiscount());

    db.insert(ClientTable.TABLE_NAME, null, values);
    db.close();
  }
Example #19
0
 private void deletePrevious(String text) {
   SQLiteOpenHelper helper = new DBHelper(activity);
   SQLiteDatabase db = null;
   try {
     db = helper.getWritableDatabase();
     db.delete(DBHelper.TABLE_NAME, DBHelper.TEXT_COL + "=?", new String[] {text});
   } finally {
     close(null, db);
   }
 }
 @Override
 public int delete(Uri uri, String selection, String[] selectionArgs) {
   SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
   SQLiteDatabase db = mOpenHelper.getWritableDatabase();
   int count = db.delete(args.table, args.where, args.args);
   if (count > 0) {
     getContext().getContentResolver().notifyChange(uri, null);
   }
   return count;
 }
Example #21
0
 void clearHistory() {
   SQLiteOpenHelper helper = new DBHelper(activity);
   SQLiteDatabase db = null;
   try {
     db = helper.getWritableDatabase();
     db.delete(DBHelper.TABLE_NAME, null, null);
   } finally {
     close(null, db);
   }
 }
Example #22
0
  @Override
  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    SqlArguments args = new SqlArguments(uri, selection, selectionArgs);

    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int count = db.update(args.table, values, args.where, args.args);
    if (count > 0) sendNotify(uri);

    return count;
  }
 @Override
 public Uri insert(Uri uri, ContentValues values) {
   String table = uri.getLastPathSegment();
   long rowId = mSqLiteOpenHelper.getWritableDatabase().insertOrThrow(table, null, values);
   if (rowId == -1) return null;
   String notify;
   if (((notify = uri.getQueryParameter(QUERY_NOTIFY)) == null || "true".equals(notify))) {
     getContext().getContentResolver().notifyChange(uri, null);
   }
   return uri.buildUpon().appendEncodedPath(String.valueOf(rowId)).build();
 }
Example #24
0
  public DataManagerImpl(Context context) {

    this.context = context;

    SQLiteOpenHelper openHelper = new OpenHelper(this.context);
    db = openHelper.getWritableDatabase();
    //  Log.i(Constants.LOG_TAG, "DataManagerImpl created, db open status: " + db.isOpen());

    priceHistoryDao = new PriceHistoryDao(db);
    itemDao = new ItemDao(db);
  }
 public int delete(Uri paramUri, String paramString, String[] paramArrayOfString) {
   SQLiteDatabase localSQLiteDatabase = mOpenHelper.getWritableDatabase();
   if (paramUri.getPathSegments().size() != 1) {
     throw new IllegalArgumentException("Unknown Uri");
   }
   if (((String) paramUri.getPathSegments().get(0)).equals("suggestions")) {
     int i = localSQLiteDatabase.delete("suggestions", paramString, paramArrayOfString);
     getContext().getContentResolver().notifyChange(paramUri, null);
     return i;
   }
   throw new IllegalArgumentException("Unknown Uri");
 }
Example #26
0
 public void update(Note note) {
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
   ContentValues cv = new ContentValues();
   cv.put("date", sdf.format(note.getDate()));
   cv.put("refer", note.getRefer());
   cv.put("content", note.getContent());
   String whereClause = "nid=? AND id=?";
   String whereArgs[] = new String[] {String.valueOf(nid), String.valueOf(note.getId())};
   SQLiteDatabase db = helper.getWritableDatabase();
   db.update("note", cv, whereClause, whereArgs);
   db.close();
 }
  /**
   * Internal method to insert a new Cell Broadcast into the database and notify observers.
   *
   * @param message the message to insert
   * @return true if the database was updated, false otherwise
   */
  boolean insertNewBroadcast(CellBroadcastMessage message) {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    ContentValues cv = message.getContentValues();

    long rowId = db.insert(CellBroadcastDatabaseHelper.TABLE_NAME, null, cv);
    if (rowId != -1) {
      return true;
    } else {
      Log.e(TAG, "failed to insert new broadcast into database");
      return false;
    }
  }
  /**
   * Internal method to delete all cell broadcasts and notify observers.
   *
   * @return true if the database was updated, false otherwise
   */
  boolean deleteAllBroadcasts() {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();

    int rowCount = db.delete(CellBroadcastDatabaseHelper.TABLE_NAME, null, null);
    if (rowCount != 0) {
      CellBroadcastReceiverApp.resetUnreadAlertCount();
      return true;
    } else {
      Log.e(TAG, "failed to delete all broadcasts");
      return false;
    }
  }
Example #29
0
  /**
   * This method is used to <strong> Open Created Database </strong> . Use this method before doing
   * any manipulation over database like <strong>Inserting , Deleting , Finding </strong> and all
   * <strong> DML </strong> Stuff.
   *
   * @throws DBXDBNotCreatedException If used this method before creating database
   */
  public void openDatabase() throws DBXDBNotCreatedException {

    if (!isDatabaseCreated) {

      throw new DBXDBNotCreatedException();
    }

    sqLiteOpenHelper = new DatabaseHelper(context);
    sqLiteDatabase = sqLiteOpenHelper.getWritableDatabase();

    isDatabaseOpened = true;
  }
  @Override
  public Uri insert(Uri uri, ContentValues values) {
    SqlArguments args = new SqlArguments(uri);

    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    final long rowId = db.insert(args.table, null, values);
    if (rowId <= 0) return null;
    else {
      // getContext().getContentResolver().notifyChange(uri, null);
    }
    uri = ContentUris.withAppendedId(uri, rowId);
    return uri;
  }