How to get the number of query strings in Android using SQLite?

How to get the number of query strings in Android using SQLite? It seems my next method is not working.

public int getFragmentCountByMixId(int mixId) { int count = 0; SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); Cursor cursor = db.rawQuery( "select count(*) from downloadedFragement where mixId=?", new String[]{String.valueOf(mixId)}); while(cursor.moveToFirst()){ count = cursor.getInt(0); } return count; } 
+48
android sqlite count row
Jun 15 2018-11-11T00:
source share
8 answers
+102
Jun 15 2018-11-11T00:
source share
 cursor.moveToNext(); cursor.getCount(); 

If moveToNext () is not called, a cursorIndexOutOfBoundException may occur.

+7
Dec 19 '12 at 10:40
source share

This would be more efficient because work for all versions:

 int numRows = DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM table_name", null); 

or

 int numRows = DatabaseUtils.queryNumEntries(db, "table_name"); 

or , if you want to get the number of rows that have a specific selection , then you should go with (added in API 11)

 public static long queryNumEntries (SQLiteDatabase db, String table, String selection) 

Thank:)

+3
Sep 11 '15 at 7:47
source share

Query for the _ID column in the table, and then call getCount on the cursor. Here is the link that I am making in one of my projects. Look at line number 110.

0
Jun 15 2018-11-11T00
source share

use string instead of int

 String strCount = ""; int count = 0; SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); Cursor cursor = db.rawQuery( "select count(*) from downloadedFragement where mixId=?", new String[]{String.valueOf(mixId)}); while(cursor.moveToFirst()){ strCount = cursor.getString(cursor.getColumnIndex("COUNT(*)")); } count = Integer.valueOf(strCount).intValue(); 
0
Jul 26 '12 at 18:17
source share

In DatabaseUtils

 public static long queryNumEntries(SQLiteDatabase db, String table) 
0
Oct 16 '15 at 7:14
source share
  public long getRecords() { return DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM contacts", null); } 

You can use this as a method or use this if your database uses auto-increment

  public long getRecords() { return DatabaseUtils.longForQuery(db, "SELECT seq FROM sqlite_sequence", null); } 
0
Nov 28 '18 at 20:24
source share
 val query = "SELECT * FROM $TABLE_NAME ;" val result = db.rawQuery(query,null) Toast.makeText(ctx,result.count,Toast.LENGTH_SHORT).show() 
0
Jan 08 '19 at 19:33
source share



All Articles