如何在android中修复sqlite数据库的编译错误

时间:2013-01-25 06:33:26

标签: android database sqlite

这行代码有问题,getInt方法编译错误。因为变量KEY_HITS是String而不是int。我该如何解决这个问题?

 total = total + c.getInt(KEY_HITS);

这里有更多的代码;

    public static final String KEY_ROWID="_id";
    public static final String KEY_NAME="persons_name";
    public static final String KEY_HITS="persons_hits";

 public int getTotal() {

String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_HITS };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
        null, null);
int total = 0;

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
    total = total + c.getInt(KEY_HITS);
}
return total;
 }

1 个答案:

答案 0 :(得分:1)

做什么错误说因为变量KEY_HITS是String而不是int
你需要在getInt()中传递整数参数。您可以按getColumnIndex()

获取列的索引
total = total + c.getInt(c.getColumnIndex(KEY_HITS));