简体   繁体   中英

How to get the count of the records for the following database in android?

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 .

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM