2013-03-06 22 views
1

如果我使用遊標,我不能在下面的代碼中使用「cursor.getString」。 我發現「Cursor.count = 0」。Android:SQLite,如何從多個表中選擇count(*)?

但是,如果我在adb shell中執行此代碼,我可以得到結果「10 | 15 |」。

我可以知道原因或如何實現我的目標?非常感謝。

select (select count(*) from table1) as count1, (select count(*) from table2) as count2; 

回答

0

你用光標在做什麼?你最近怎麼樣? (內容解析器,exec原始sql等等)。

more correct做選擇計數的方法,因爲相關鏈接清楚地顯示。

0

所以通常使用兩個查詢

String[] queries = {"select count(*) from table1", 
        "select count(*) from table2"}; 

然後使用Cursor,進行查詢和getter方法獲取價值。

Cursor c = null; 
int index = 0; 
int totalCount = 0; 
while (index < queries.length) { 
    c = db.rawQuery(queries[index], null); 
    if (c.moveToFirst()) { 
     totalCount += c.getInt(0); 
    } 
    index++; 
} 
+0

這不會提供兩個表中各個行的計數。錯誤的答案。 – ErikE 2013-03-06 18:00:01

+0

解答已更新。 – Sajmon 2013-03-06 18:27:05