簡體   English   中英

如何計算Android中sqlite中的記錄數

[英]How to count number of records in sqlite in Android

我正在研究android項目。 我使用了sqlite數據庫。 我在數據庫中有表。 我創建了databasehelper類。 我想計算特定表中的記錄數。 我怎么能實現這個目標? 任何幫助將不勝感激?

這會更有效:

int numRows = DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM table_name", null);

要么:

int numRows = DatabaseUtils.queryNumEntries(db, "table_name");

使用SELECT COUNT(*) FROM table_name查詢和, 計算Cursor的大小 ..

從Cursor到count size(游標中的行數)的方法是,

getCount()

返回游標中的行數。

要么:

DatabaseUtils使用方法queryNumEntries(SQLiteDatabase db, String table)

喜歡,

long numberOfRows = DatabaseUtils.queryNumEntries(db, "table_name");

返回表中的行數

試試這個。

Cursor c = db.rawQuery("select * from your_table_name",null);
Log.i("Number of Records"," :: "+c.getCount());

編輯: c.getCount()返回特定表的記錄數。

在科特林

fun getFruitTableCount(): Int {
        val db = readableDatabase
        val numRows = DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM " + DBContract.FruitEntry.TABLE_NAME, null).toInt()
        return numRows
    }

請試試這個:

在您的DatabaseHelper.java中:

public Cursor getYourTableContents() {
  SQLiteDatabase db = this.getWritableDatabase();

  Cursor data = db.rawQuery("SELECT * FROM " + "Your table name", null);

  return data;
}

要獲取表中的行數:

Cursor yourCursor = myDB.getYourTableContents();

int i = 0;

while (yourCursor.moveToNext()) {
  i += 1;
}

i是你的行數整數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM