如何计算表中的记录并将值输入活动?

时间:2016-08-22 14:29:58

标签: android sqlite android-sqlite

我尝试在我的DBHandler类中使用此代码

public int getPointsCount() {
        String countQuery = "SELECT * FROM " + TABLE_STUDENTS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
        return cursor.getCount();
    }

我在主Activity中的一个按钮中使用了这个调用,但是当我点击Button时它会导致我的应用程序崩溃

callButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             int cn =  countStudents();
                studentNumberText.setText(cn);
            }
        });

public int countStudents (){
        DBHandler dbHandler = new DBHandler(this, null, null, 1);
        int c = dbHandler.getPointsCount();
        return c;
    }

1 个答案:

答案 0 :(得分:0)

通过调用此方法获取表计数

public int gettablecount(){
    Cursor mCount= db.rawQuery("select count(*) from TABLENAME ", null);
    mCount.moveToFirst();
    int count= mCount.getInt(0);
    mCount.close();
    return  count;
}