簡體   English   中英

如何修復Android中的sqlite數據庫的編譯器錯誤

[英]how to fix compiler error for sqlite database in android

這行代碼有問題,getInt方法上的編譯器錯誤。 因為變量KEY_HITS是字符串而不是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;
 }

執行錯誤提示, 因為變量KEY_HITS是String而不是int
您需要在getInt()中傳遞整數參數。 您可以通過getColumnIndex()獲取列的索引

total = total + c.getInt(c.getColumnIndex(KEY_HITS));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM