簡體   English   中英

如何在android中獲取以下數據庫的記錄數?

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

我創建了這樣的數據庫...

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

我正在使用 api23。 請建議代碼。

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;
}

它返回您計算表中的總記錄數。

編輯

您已經在使用SQLiteDatabase db; 在你的代碼中。 因此,使用這些行來獲取記錄總數。 僅使用這 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