我正在尝试从我的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;
}
感谢。
答案 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)