2017-08-24 22 views
0

我寫了一個應用程序,其中包含一個SQLite數據庫,並有一個名爲「通知」的表。
我想通過使用上述表中名爲「packageName」的列來獲取記錄計數。如何通過使用光標中的特定列來統計記錄數量?

我該怎麼做?

代碼:

public long getRecordsCount() { 
    SQLiteDatabase db = this.getReadableDatabase(); 
    long count = DatabaseUtils.queryNumEntries(db, CNotificationDataBaseTableDeclarations.NotificationsTable.TABLE_NAME); 
    db.close(); 
    return count; 
} 
+0

sqlite3的有一個'count'功能'SELECT COUNT(*)從Table' –

+0

我使用[如何獲得記錄:從SQLite的總計數(HTTPS的源碼 – Niraj

+0

可能重複:// stackoverflow.com/questions/5925790/how-to-get-the-total-count-of-records-from-sqlite) –

回答

0

試試這個:

public long getRecordsCount() { 
    SQLiteDatabase db = getReadableDatabase(); 
    try { 
     String selectQuery = 
       "SELECT COUNT(*)" + 
       " FROM " + CNotificationDataBaseTableDeclarations.NotificationsTable.TABLE_NAME + 
       " WHERE " + YOUR_SPECIFIC_COLUMN + " = 'COLUMN_VALUE'"; 
     return DatabaseUtils.longForQuery(db, selectQuery, null); 
    } finally { 
     db.close(); 
    } 
} 
+0

其工作....謝謝 – Niraj

0

還有就是queryNumEntries()一個版本,它允許您使用相同的selection過濾與正常查詢:

long count = DatabaseUtils.queryNumEntries(db, 
        CNotificationDataBaseTableDeclarations.NotificationsTable.TABLE_NAME, 
        YOUR_SPECIFIC_COLUMN + " = 42", null); 

或字符串va攻略:

    ... 
        YOUR_SPECIFIC_COLUMN + " = ?", new String[]{ "your value" }); 
相關問題