我調用SQLiteDatabase.query並獲得一個Cursor。雖然我還在遍歷該遊標,但是可以安全地從同一個線程發出其他查詢嗎?執行新的SQLite查詢是否安全,同時仍在迭代遊標以查詢以前的查詢? (Android)
4
A
回答
2
是的。您可以從同一個線程中獲得多個未完成的查詢並獨立訪問每個遊標。
我下面的代碼添加到測試活動,並運行正常:
SQLiteDatabase connection = getApplicationContext().openOrCreateDatabase("foo.db", MODE_PRIVATE, null);
try {
connection.execSQL("drop table if exists person");
connection.execSQL("create table person (id integer, name string)");
ContentValues cv = new ContentValues();
cv.put("id", 1);
cv.put("name", "leo");
connection.insert("person", null, cv);
cv = new ContentValues();
cv.put("id", 2);
cv.put("name", "yui");
connection.insert("person", null, cv);
Cursor rs = connection.query("person", new String[] {"id", "name" }, null, null, null, null, null);
while(rs.moveToNext())
{
System.out.println("name = " + rs.getString(1));
System.out.println("id = " + rs.getString(0));
Cursor rs2 = connection.query("person", new String[] {"id", "name" }, null, null, null, null, null);
while (rs2.moveToNext()) {
System.out.println("name = " + rs2.getString(1));
System.out.println("id = " + rs2.getString(0));
}
}
} catch (Exception e) {
System.out.println("Exception " + e);
}
3
相關問題
- 1. 查詢此查詢是否安全?
- 2. 何時執行遊標中的查詢?
- 3. 的Android SQlite的遊標查詢錯誤
- 4. 執行SQLite查詢
- 5. Android:是否可以在光標上執行查詢?
- 6. 根據以前的查詢執行選擇查詢
- 7. 是否可以查詢查詢?
- 8. 遊標,SQLite和查詢
- 9. 是否可以剖析SQLite SQL查詢?
- 10. 是否可以在查詢的ColdFusion查詢中執行字符串連接?
- 11. 通過查詢迭代的sql查詢
- 12. 執行多行SQLite查詢
- 13. 查詢SQLite/Android中的查詢
- 14. 查詢迭代的
- 15. parse.com查詢SQLite的查詢
- 16. MySQL查詢SQLite的查詢
- 17. Sqlite查詢Android
- 18. Android Sqlite查詢
- 19. Android sqlite查詢
- 20. SQLite Android查詢
- 21. ssis同時執行查詢
- 22. 如何理解一行是否被以前的select查詢查詢過?
- 23. 執行sqlite查詢異步
- 24. JDBC/Spring - 執行sql查詢以獲得結果的迭代器
- 25. 訪問終端以執行Android查詢
- 26. 如何檢索先前查詢的查詢執行時間
- 27. 安全查詢
- 28. 在Android代碼中查詢SQLite時CursorIndexOutOfBoundsException
- 29. android SQLITE查詢不同
- 30. sqlite android查詢(光標)
我認爲你接受的答案是不正確。請參閱下面我的答案中的示例代碼。 – ditkin
在執行這兩個光標將不會在同一時間得到執行,outter光標正在運行時,內部光標將在等待位置和副版本@Travis – Lucifer
我試圖ditkin的代碼和它的工作原理:第一個查詢不會失去自己的位置時,第二個查詢正在執行,這是我所擔心的(或者更糟糕的是,一個異常或死鎖)。我不確定你指的是什麼等待位置。只有一個線程,所以顯然不會有任何併發或後臺執行。 – Travis