如何使用 SQLite 在 Android 中获取查询的行数?
如何使用 SQLite 在 Android 中获取查询的行数?看来我下面的方法行不通。
public int getFragmentCountByMixId(int mixId) {
int count = 0;
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
Cursor cursor = db.rawQuery(
"select count(*) from downloadedFragement where mixId=?",
new String[]{String.valueOf(mixId)});
while(cursor.moveToFirst()){
count = cursor.getInt(0);
}
return count;
}
How do I get the row count of a query in Android using SQLite? It seems my following method does not work.
public int getFragmentCountByMixId(int mixId) {
int count = 0;
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
Cursor cursor = db.rawQuery(
"select count(*) from downloadedFragement where mixId=?",
new String[]{String.valueOf(mixId)});
while(cursor.moveToFirst()){
count = cursor.getInt(0);
}
return count;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(8)
Cursor.getCount()
Cursor.getCount()
如果不调用moveToNext(),可能会出现cursorIndexOutOfBoundException。
If the moveToNext() is not called, the cursorIndexOutOfBoundException may arise.
这会更有效,因为适用于所有版本:
或
或如果您想获取特定选择的行数,那么您应该使用(在 API 11 中添加)
谢谢:)
This would be more efficient because work for all versions:
or
or if you want to get number of rows which specific selection then you should go with (added in API 11)
Thanks :)
使用 String 代替 int
use String instead of int
查询表中的 _ID 列,然后在游标上调用 getCount。这是我正在做的链接在我的一个项目中。查看第 110 行。
Query for _ID column in table and then call getCount on cursor. Here is a link I am doing in one of my project. Look at line number 110.
在DatabaseUtils中
In
DatabaseUtils
您可以使用它作为一种方法,或者如果您的数据库使用自动增量,则可以使用它
You can use this as a method or use this if your database uses auto increment