我想在我的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();
}
使用此代碼,我用在主要活動按鈕這一呼籲,但它會導致我的應用程序崩潰,當我按一下按鈕
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;
}
SELECT COUNT(*)獲取表計數://www.w3schools.com/sql/sql_func_count.asp – kosa
'它會導致應用程序crash' =交你的logcat –