简体   繁体   English

如何在android中获取以下数据库的记录数?

[英]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.我正在使用 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;您已经在使用SQLiteDatabase db; in your code.在你的代码中。 So use these line for getting total count of records.因此,使用这些行来获取记录总数。 Only use these 2 lines.仅使用这 2 行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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