0

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

这是代码:

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

谢谢。

4

2 回答 2

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;
}
于 2013-06-18T13:08:28.477 回答
4

试试c.getCount()..

参考

希望这可以帮助..

于 2013-06-18T13:09:34.473 回答