這是我第一次使用數據庫,我不確定這是如何工作的。我做了數據庫,並做了一個查詢,返回一個遊標和...現在什麼?真的是什麼光標?我可以使用它來瀏覽我的數據,還是必須將它放入ArrayList或ListActivity中?在SQLite查詢後如何處理光標?
3
A
回答
6
您需要迭代遊標以獲得結果。
使用cursor.moveToFirst()
和/或cursor.moveToNext()
(帶有一個while循環)。然後,您可以使用getX()方法,如cursor.getInt()
或cursor.getString()
。
例如,IR您從您的查詢期待一個結果:
if (cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex('NAME'));
int age = cursor.getInt(cursor.getColumnIndex('AGE'));
} else {
// oops nothing found!
}
0
2
首先調用cursor.moveToFirst()。每次調用cursor.moveToNext()時,它都會移動到下一行。確保當你用光標完成時,調用cursor.deactivate(),否則你的日誌貓會出錯。
0
from Developer.android:該接口提供對數據庫查詢返回的結果集的隨機讀寫訪問。
換句話說:查詢返回一組由遊標表示的數據。首先,你需要確保你有一個有效的遊標(非空),然後嘗試將其移動到數據集中的所需位置(使用moveToXXX方法)。爲了獲得遊標指向的數據,使用getXXX方法。完成使用後,請確保近距離呼叫釋放資源。
1
遍歷返回遊標實例
public List<Object[]> cursorToTableRows(Cursor cursor) {
List<Object[]> result = new ArrayList<Object[]>(cursor.getCount());
cursor.move(0);
cursor.moveToNext();
while (cursor.isAfterLast() == false) {
Object[] tableRow = new Object[cursor.getColumnCount()];
for(int i=0; i<cursor.getColumnNames().length; i++) {
int columnIndex = cursor.getColumnIndex(cursor.getColumnName(i));
String columnValue = cursor.getString(columnIndex);
tableRow[i] = columnValue;
}
result.add(tableRow);
cursor.moveToNext();
}
cursor.close();
return result;
}
然後創建所需的對象。
public List<Vehicle> getVehicles() {
List<Vehicle> vehicles = new ArrayList<Vehicle>();
Cursor cursor = null;
List<Object[]> objects = cursorToTableRows(cursor);
for(Object[] row : objects) {
int i=0;
Vehicle vehicle = new Vehicle(row[i++].toString(), row[i++].toString()));
vehicles.add(vehicle)
}
return vehicles;
}
0
你成功有你光標設置後,您通常會希望顯示以某種形式的視圖。
看一看下面的答案使用光標適配器配對了一個詳細的,但簡單的例子,你的新崛起光標成你想要的XML視圖:
相關問題
- 1. sqlite android查詢(光標)
- 2. 查詢並與SQLite的光標在Android
- 3. SQLite如何處理大規模查詢?
- 4. 從光標檢索SQLite查詢數據
- 5. SQLIte查詢返回空的光標
- 6. 處理Android中的無效SQLite查詢
- 7. SQLite SQL查詢未處理的行
- 8. SQLite數據庫查詢異常處理
- 9. 查詢中更改光標後 - onStart()?
- 10. 如何處理\查詢mysql
- 11. 如何處理MySQL查詢
- 12. 如何處理查詢?
- 13. 光標和查詢
- 14. 爲什麼我的光標在這個SQLite數據庫查詢後是空的?
- 15. 在成功完成SQLite數據庫查詢後,Android光標保持空白
- 16. 的Android SQLite的查詢和使用遊標來處理多行
- 17. 在處理中更改鼠標光標
- 18. SQLite和光標
- 19. 我如何理解sqlite查詢計劃?
- 20. 錯誤處理光標
- 21. 查詢完成後,如何後處理Excel Web查詢中的數據?
- 22. 如何在查詢後過濾流星光標,然後發佈結果
- 23. 如何在處理請求後向數據庫發送查詢?
- 24. 我如何在處理它後獲取SQL查詢ID?
- 25. 如何處理SQLite的
- 26. 查詢處理
- 27. 未找到與查詢條件匹配的SQLite查詢調用的光標
- 28. android sqlite查詢返回光標0結果
- 29. sqlite光標行爲
- 30. 光標'訂購'SQLite
會這是查看數據庫中有多少記錄是最好的方法? – NoBugs
不,請使用計數。 – aromero