How to count number of records in sqlite in Android

后端 未结 5 785
礼貌的吻别
礼貌的吻别 2020-12-16 13:05

I am working on android project. I used sqlite database for it. I have tables in database. I have created databasehelper class. i want to count number of records in particul

5条回答
  •  既然无缘
    2020-12-16 14:11

    Please try this:

    In your DatabaseHelper.java:

    public Cursor getYourTableContents() {
      SQLiteDatabase db = this.getWritableDatabase();
    
      Cursor data = db.rawQuery("SELECT * FROM " + "Your table name", null);
    
      return data;
    }
    

    To get the number of rows in your table:

    Cursor yourCursor = myDB.getYourTableContents();
    
    int i = 0;
    
    while (yourCursor.moveToNext()) {
      i += 1;
    }
    

    i is your rows count as an integer.

提交回复
热议问题