Anyone knows a code in Sqlite that you say a value and it checks how many of those are in the column_x? Imagine you say value Johnny , and it checks how many Johnnys are in the column_names.
Asked
Active
Viewed 37 times
-1
-
2where your approach ?? Search `like` & `count` in sqlite – IntelliJ Amiya Jul 14 '17 at 12:31
-
I don't know , im very noob on android thats why im asking – Emanuel Sobreiro Jul 14 '17 at 12:32
-
You want to do a [count](https://sqlite.org/lang_aggfunc.html#count) operation. With a where clause. Search about aggregation functions. – litelite Jul 14 '17 at 12:33
-
@EmanuelSobreiro read https://stackoverflow.com/a/5381457/3395198 – IntelliJ Amiya Jul 14 '17 at 12:35
-
1Use Query=select COUNT() from tbl_name where column_x=Johnny get count Like this Cursor cursor = db.rawQuery(Query, null); cursor.moveToFirst(); int count = cursor.getInt(0); cursor.close(); db.close(); – KuLdip PaTel Jul 14 '17 at 12:39
1 Answers
2
Just pass your column value which you want to count that how many time it present...
public long getCount(String column_x_Value) {
long count;
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + column_x + "='" + column_x_Value+ "'", null);
//here column_x name of column and column_x_Value which is to be counted
count = c.getCount();
if (c != null && !c.isClosed())
c.close();
return count;
}

Mohd Saquib
- 580
- 4
- 14
-
1Too much code. `SELECT Count(Name) FROM Names WHERE Name = 'Johnny'` would directly return the count, without any extra cursor operation. – Phantômaxx Jul 14 '17 at 12:46