从SQLite获取行数

时间:2013-06-18 13:04:42

标签: android sqlite count

我正在尝试从我的Android应用程序的sqlite获取行数,但由于某种原因,即使我的数据库中有足够的行数,它总是返回Zero。

以下是代码:

DatabaseConnector database1 = new DatabaseConnector(getApplicationContext());
int TotalPosts = database1.getIds();


 public int getIds()
       {
           String selectQuery = "SELECT COUNT(Pst_id) AS CountTotal FROM student_posts";           
           SQLiteDatabase database = this.dbOpenHelper.getReadableDatabase();
           Cursor c = database.rawQuery(selectQuery, null);     
           count = c.getColumnIndex("CountTotal");    
           return count;
       }

感谢。

2 个答案:

答案 0 :(得分:10)

尝试如下:

public int getIds()
    {
    String selectQuery = "SELECT Pst_id FROM student_posts";           
    SQLiteDatabase database = this.dbOpenHelper.getReadableDatabase();
    Cursor c = database.rawQuery(selectQuery, null);
    c.moveToFirst();
    int total = c.getCount();
    c.close();

    return total;
}

答案 1 :(得分:4)

试试c.getCount() ..

Reference

希望这会有所帮助..