-4

I created the database like this...

SQLiteDatabase db;
db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS student(idno VARCHAR,name VARCHAR,class VARCHAR,year VARCHAR,gender VARCHAR);");

I am using api23. please suggest the code .

Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
Shaik Salam
  • 51
  • 1
  • 11
  • 2
    Possible duplicate of [SQLite Query in Android to count rows](http://stackoverflow.com/questions/5202269/sqlite-query-in-android-to-count-rows) – Mike M. Sep 12 '16 at 09:41

1 Answers1

1
public int getDBcountField () {
        // select query
        SQLiteDatabase db = this.getWritableDatabase();

        String sql = "SELECT * FROM student";
        int recordCount = db.rawQuery(sql, null).getCount();
        db.close();
        return recordCount;
}

It returns you to count number of total records in the table.

EDIT

You already use SQLiteDatabase db; in your code. So use these line for getting total count of records. Only use these 2 lines.

String sql = "SELECT * FROM student";
int recordCount = db.rawQuery(sql, null).getCount();
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
  • its showing "cannot resolve method getWritabaleDatabase()" – Shaik Salam Sep 12 '16 at 09:48
  • okkk.. show me.. where is you use this code. If you use in any inner class then `this` will represent inner class. Better solution is put this whole method in your DatabaseHandler Class and call from your activity(). – Jitesh Prajapati Sep 12 '16 at 09:52
  • If You use this code in your activity then just replace `this.getWritableDatabase();` with `new your_databasehelper_class.getWritableDatabase();`. – Jitesh Prajapati Sep 12 '16 at 09:55
  • I created the database like this db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null); db.execSQL("CREATE TABLE IF NOT EXISTS student(idno VARCHAR,name VARCHAR,class VARCHAR,year VARCHAR,gender VARCHAR);"); After inserting then on click i called the the method which you have given – Shaik Salam Sep 12 '16 at 09:57
  • see my edit. `this.getWritableDatabase();` is used for instance/object of `SQLiteDatabase` but you already use `db` as SQLiteDatabase's object. So now only write these lines it will returns total count. Remeber db should not be null; – Jitesh Prajapati Sep 12 '16 at 10:04