Java Code Examples for net.sqlcipher.database.SQLiteDatabase#rawQuery()

The following examples show how to use net.sqlcipher.database.SQLiteDatabase#rawQuery() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public boolean columnExists(SQLiteDatabase db, String tableName, String column) {
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("PRAGMA table_info(" + tableName + ")", null);
        int nameColumnIndex = cursor.getColumnIndexOrThrow("name");
        while (cursor.moveToNext()) {
            String name = cursor.getString(nameColumnIndex);
            if (name.equals(column)) {
                return true;
            }
        }
        return false;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 2
private static Set<String> findUnusedInGroupMembership(@NonNull SQLiteDatabase db, Set<String> candidates) {
  Set<String> unused = new HashSet<>(candidates);

  try (Cursor cursor = db.rawQuery("SELECT members FROM groups", null)) {
    while (cursor != null && cursor.moveToNext()) {
      String   serializedMembers = cursor.getString(cursor.getColumnIndexOrThrow("members"));
      String[] members           = DelimiterUtil.split(serializedMembers, ',');

      for (String member : members) {
        if (unused.remove(member)) {
          Log.i(TAG, "Recipient " + member + " was found in a group membership list.");
        }
      }
    }
  }

  return unused;
}
 
Example 3
public long getThreadIdForMessage(long id) {
  String sql        = "SELECT " + THREAD_ID + " FROM " + TABLE_NAME + " WHERE " + ID + " = ?";
  String[] sqlArgs  = new String[] {id+""};
  SQLiteDatabase db = databaseHelper.getReadableDatabase();

  Cursor cursor = null;

  try {
    cursor = db.rawQuery(sql, sqlArgs);
    if (cursor != null && cursor.moveToFirst())
      return cursor.getLong(0);
    else
      return -1;
  } finally {
    if (cursor != null)
      cursor.close();
  }
}
 
Example 4
public Cursor getRecentPushConversationList(int limit, boolean includeInactiveGroups) {
  SQLiteDatabase db               = databaseHelper.getReadableDatabase();
  String         activeGroupQuery = !includeInactiveGroups ? " AND " + GroupDatabase.TABLE_NAME + "." + GroupDatabase.ACTIVE + " = 1" : "";
  String         where            = MESSAGE_COUNT + " != 0 AND " +
                                    "(" +
                                      RecipientDatabase.REGISTERED + " = " + RecipientDatabase.RegisteredState.REGISTERED.getId() + " OR " +
                                      "(" +
                                        GroupDatabase.TABLE_NAME + "." + GroupDatabase.GROUP_ID + " NOT NULL AND " +
                                        GroupDatabase.TABLE_NAME + "." + GroupDatabase.MMS + " = 0" +
                                        activeGroupQuery +
                                      ")" +
                                    ")";
  String         query = createQuery(where, limit);

  return db.rawQuery(query, null);
}
 
Example 5
public @NonNull Map<RecipientId, Integer> getInboxPositions() {
  SQLiteDatabase db     = databaseHelper.getReadableDatabase();
  String         query  = createQuery(MESSAGE_COUNT + " != ?", 0);

  Map<RecipientId, Integer> positions = new HashMap<>();

  try (Cursor cursor = db.rawQuery(query, new String[] { "0" })) {
    int i = 0;
    while (cursor != null && cursor.moveToNext()) {
      RecipientId recipientId = RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.RECIPIENT_ID)));
      positions.put(recipientId, i);
      i++;
    }
  }

  return positions;
}
 
Example 6
public long getThreadIdForMessage(long id) {
  String sql        = "SELECT " + THREAD_ID + " FROM " + TABLE_NAME + " WHERE " + ID + " = ?";
  String[] sqlArgs  = new String[] {id+""};
  SQLiteDatabase db = databaseHelper.getReadableDatabase();

  Cursor cursor = null;

  try {
    cursor = db.rawQuery(sql, sqlArgs);
    if (cursor != null && cursor.moveToFirst())
      return cursor.getLong(0);
    else
      return -1;
  } finally {
    if (cursor != null)
      cursor.close();
  }
}
 
Example 7
private static @Nullable Long getRecipientId(@NonNull SQLiteDatabase db, @NonNull String address) {
  try (Cursor cursor = db.rawQuery("SELECT _id FROM recipient_preferences WHERE recipient_ids = ?", new String[]{ address })) {
    if (cursor != null && cursor.moveToFirst()) {
      return cursor.getLong(cursor.getColumnIndex("_id"));
    } else {
      return null;
    }
  }
}
 
Example 8
private Cursor rawQuery(@NonNull String where, @Nullable String[] arguments) {
  SQLiteDatabase database = databaseHelper.getReadableDatabase();
  return database.rawQuery("SELECT " + Util.join(MMS_PROJECTION, ",") +
                           " FROM " + MmsDatabase.TABLE_NAME +  " LEFT OUTER JOIN " + AttachmentDatabase.TABLE_NAME +
                           " ON (" + MmsDatabase.TABLE_NAME + "." + MmsDatabase.ID + " = " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.MMS_ID + ")" +
                           " WHERE " + where + " GROUP BY " + MmsDatabase.TABLE_NAME + "." + MmsDatabase.ID, arguments);
}
 
Example 9
public @Nullable
ViewOnceExpirationInfo getNearestExpiringViewOnceMessage() {
  SQLiteDatabase       db                = databaseHelper.getReadableDatabase();
  ViewOnceExpirationInfo info              = null;
  long                 nearestExpiration = Long.MAX_VALUE;

  String   query = "SELECT " +
                       TABLE_NAME + "." + ID + ", " +
                       VIEW_ONCE + ", " +
                       DATE_RECEIVED + " " +
                   "FROM " + TABLE_NAME + " INNER JOIN " + AttachmentDatabase.TABLE_NAME + " " +
                       "ON " + TABLE_NAME + "." + ID + " = " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.MMS_ID + " " +
                   "WHERE " +
                       VIEW_ONCE + " > 0 AND " +
                       "(" + AttachmentDatabase.DATA + " NOT NULL OR " + AttachmentDatabase.TRANSFER_STATE + " != ?)";
  String[] args = new String[] { String.valueOf(AttachmentDatabase.TRANSFER_PROGRESS_DONE) };

  try (Cursor cursor = db.rawQuery(query, args)) {
    while (cursor != null && cursor.moveToNext()) {
      long id              = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
      long dateReceived    = cursor.getLong(cursor.getColumnIndexOrThrow(DATE_RECEIVED));
      long expiresAt       = dateReceived + ViewOnceUtil.MAX_LIFESPAN;

      if (info == null || expiresAt < nearestExpiration) {
        info              = new ViewOnceExpirationInfo(id, dateReceived);
        nearestExpiration = expiresAt;
      }
    }
  }

  return info;
}
 
Example 10
public Cursor queryMessages(@NonNull String query) {
  SQLiteDatabase db                  = databaseHelper.getReadableDatabase();
  String         fullTextSearchQuery = createFullTextSearchQuery(query);

  if (TextUtils.isEmpty(fullTextSearchQuery)) {
    return null;
  }

  Cursor cursor = db.rawQuery(MESSAGES_QUERY, new String[] { fullTextSearchQuery,
                                                             fullTextSearchQuery });

  setNotifyConverationListListeners(cursor);
  return cursor;
}
 
Example 11
private static void dropAllTables(@NonNull SQLiteDatabase db) {
  try (Cursor cursor = db.rawQuery("SELECT name, type FROM sqlite_master", null)) {
    while (cursor != null && cursor.moveToNext()) {
      String name = cursor.getString(0);
      String type = cursor.getString(1);

      if ("table".equals(type) && !name.startsWith("sqlite_")) {
        db.execSQL("DROP TABLE IF EXISTS " + name);
      }
    }
  }
}
 
Example 12
Source Project: commcare-android   File: SqlStorage.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean isTableExist(SQLiteDatabase db, String table) {
    Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + table + "'", null);
    if (cursor != null) {
        if (cursor.getCount() > 0) {
            cursor.close();
            return true;
        }
        cursor.close();
    }
    return false;
}
 
Example 13
public @NonNull List<RecipientId> getUninvitedRecipientsForInsights() {
  SQLiteDatabase    db      = databaseHelper.getReadableDatabase();
  List<RecipientId> results = new LinkedList<>();
  final String[]    args    = new String[]{String.valueOf(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(31))};

  try (Cursor cursor = db.rawQuery(INSIGHTS_INVITEE_LIST, args)) {
    while (cursor != null && cursor.moveToNext()) {
      results.add(RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID))));
    }
  }

  return results;
}
 
Example 14
private Cursor getConversationList(String archived) {
  SQLiteDatabase db     = databaseHelper.getReadableDatabase();
  String         query  = createQuery(ARCHIVED + " = ? AND " + MESSAGE_COUNT + " != 0", 0);
  Cursor         cursor = db.rawQuery(query, new String[]{archived});

  setNotifyConverationListListeners(cursor);

  return cursor;
}
 
Example 15
public @NonNull Cursor getGalleryMediaForThread(long threadId, @NonNull Sorting sorting) {
  SQLiteDatabase database = databaseHelper.getReadableDatabase();
  String         query    = sorting.applyToQuery(applyEqualityOperator(threadId, GALLERY_MEDIA_QUERY));
  String[]       args     = {threadId + ""};
  Cursor         cursor   = database.rawQuery(query, args);
  setNotifyConverationListeners(cursor, threadId);
  return cursor;
}
 
Example 16
public @NonNull Cursor getDocumentMediaForThread(long threadId, @NonNull Sorting sorting) {
  SQLiteDatabase database = databaseHelper.getReadableDatabase();
  String         query    = sorting.applyToQuery(applyEqualityOperator(threadId, DOCUMENT_MEDIA_QUERY));
  String[]       args     = {threadId + ""};
  Cursor         cursor   = database.rawQuery(query, args);
  setNotifyConverationListeners(cursor, threadId);
  return cursor;
}
 
Example 17
public static boolean columnExists(@NonNull SQLiteDatabase db, @NonNull String table, @NonNull String column) {
  try (Cursor cursor = db.rawQuery("PRAGMA table_info(" + table + ")", null)) {
    int nameColumnIndex = cursor.getColumnIndexOrThrow("name");

    while (cursor.moveToNext()) {
      String name = cursor.getString(nameColumnIndex);

      if (name.equals(column)) {
        return true;
      }
    }
  }

  return false;
}
 
Example 18
public @NonNull Cursor getAllMediaForThread(long threadId, @NonNull Sorting sorting) {
  SQLiteDatabase database = databaseHelper.getReadableDatabase();
  String         query    = sorting.applyToQuery(applyEqualityOperator(threadId, ALL_MEDIA_QUERY));
  String[]       args     = {threadId + ""};
  Cursor         cursor   = database.rawQuery(query, args);
  setNotifyConverationListeners(cursor, threadId);
  return cursor;
}
 
Example 19
public StorageBreakdown getStorageBreakdown() {
  StorageBreakdown storageBreakdown = new StorageBreakdown();
  SQLiteDatabase   database         = databaseHelper.getReadableDatabase();

  try (Cursor cursor = database.rawQuery(UNIQUE_MEDIA_QUERY, new String[0])) {
    int sizeColumn        = cursor.getColumnIndexOrThrow(AttachmentDatabase.SIZE);
    int contentTypeColumn = cursor.getColumnIndexOrThrow(AttachmentDatabase.CONTENT_TYPE);

    while (cursor.moveToNext()) {
      int    size = cursor.getInt(sizeColumn);
      String type = cursor.getString(contentTypeColumn);

      switch (MediaUtil.getSlideTypeFromContentType(type)) {
        case GIF:
        case IMAGE:
        case MMS:
          storageBreakdown.photoSize += size;
          break;
        case VIDEO:
          storageBreakdown.videoSize += size;
          break;
        case AUDIO:
          storageBreakdown.audioSize += size;
          break;
        case LONG_TEXT:
        case DOCUMENT:
          storageBreakdown.documentSize += size;
          break;
        default:
          break;
      }
    }
  }

  return storageBreakdown;
}
 
Example 20
public Cursor getData(int id) {
    SQLiteDatabase db = this.getReadableDatabase(DB_PASSWORD);
    Cursor res = db.rawQuery("select * from person where id=" + id + "", null);
    return res;
}