如何做一个光标指删除的行? [英] How does a cursor refer to deleted rows?

查看:159
本文介绍了如何做一个光标指删除的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我得到一个光标从Android中似乎包含的行固定数量并不无论发生了什么DB改变一个SQLite数据库查询回来。如果,例如,某些行被删除,而游标打开我仍然可以参考已经被删除的行。这是很好的,但它是如何工作的?

When I get a Cursor back from a sqlite DB query in Android it seems to contain a fixed number of rows that does not change regardless of what happens to the DB. If, for example, some of the rows are deleted while the Cursor is open I can still reference rows that have been deleted. This is nice, but how does it work?

一个猜测可能是sqlite的都知道我有一个指向该行中的光标等保持旧数据的副本中。但是,如果我有一百万行,删除它们,并与一百万个不同的行替换他们,好像很多数据缓存!

One guess might be that sqlite knows I have a Cursor that is pointing to that row and so keeps a copy of the old data around. But what if I had a million rows, deleted them and replaced them with a million different rows, seems like a lot of data to cache!

更新:

我现在想,这种缓存我们在Android中看到的不是真正的防弹,而不是SQLite的一部分。我创建了一个测试,建立一个数据库,它1,000,000行,然后我问打印出一些成果数据库,并留下了游标打开,接下来我删除了一半的行,最后我访问了我敞开,结果光标是崩溃:

I am now thinking that this caching we see in Android isn't really bullet-proof and isn't part of SQLite. I created a test which builds a database with 1,000,000 rows in it, then I queried the db printed out some results and left the cursor open, next I deleted half the rows, finally I tried to access the cursor that I left open and the result is a crash:

04-05 18:17:16.141 E/AndroidRuntime(19655): java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
04-05 18:17:16.141 E/AndroidRuntime(19655):     at android.database.CursorWindow.nativeGetLong(Native Method)
04-05 18:17:16.141 E/AndroidRuntime(19655):     at android.database.CursorWindow.getLong(CursorWindow.java:507)
04-05 18:17:16.141 E/AndroidRuntime(19655):     at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
04-05 18:17:16.141 E/AndroidRuntime(19655):     at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:220)
04-05 18:17:16.141 E/AndroidRuntime(19655):     at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)
04-05 18:17:16.141 E/AndroidRuntime(19655):     at com.jacob.DirectDbActivity$4.run(DirectDbActivity.java:88)

这似乎是在Android中的一个错误,因为很多数据库是小的缓存窗口可以容纳所有的结果,但在大型数据库这会打你。

This seems like a bug in Android, since most databases are small the cache window can hold all the results but in large databases this is going to hit you.

推荐答案

上次我检查,游标是基于创建它的SQL数据库的快照。很像一个数据集。如果更改了数据库中的游标可能不知道,他们的快照是旧的。您可以对光标进行重新查询基础上创建光标原来的SQL来获得新的数据。

Last I checked, a cursor is a snapshot of the database based on the SQL that created it. Much like a Data set. If you change the database the cursors may not know that their snapshot is old. You can perform a re-query on the cursor to get fresh data based on the original SQL that created the cursor.

这篇关于如何做一个光标指删除的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆