Fro*_*yon 15 sql database sqlite android cursor
我在我的Android应用程序中运行SQLite3数据库.我刚刚从预先填充的数据库中读取了200k行和14列.参赛作品是单词.所有列的数据类型都是文本.查询最多11个字母的单词(例如,ABANDONMENT)工作正常.但对于12或更高(例如,ABANDONMENTS),应用程序崩溃.这是logcat:
Could not allocate CursorWindow '//data//data//com.example.myapp//databases//database.sqlite' of size 2097152 due to error -12.
threadid=11: thread exiting with uncaught exception (group=0x40adf9f0)
FATAL EXCEPTION: Thread-2883
android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=861 (# cursors opened by this proc=861)
at android.database.CursorWindow.<init>(CursorWindow.java:104)
at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:162)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:161)
at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:201)
at com.example.myapp.MainActivity.query(MainActivity.java:815)
at com.example.myapp.MainActivity$2.run(MainActivity.java:356)
at java.lang.Thread.run(Thread.java:856)
Run Code Online (Sandbox Code Playgroud)
码:
query = "select * from words where col_1 = \"" + (myWord)+ "\";";
cursor = database.rawQuery(query, null);
if (cursor != null)
cursor.moveToFirst(); // line 815
if (!cursor.isAfterLast()) {
do {
for (i = 1; i < cursor.getColumnCount(); i++) {
temp = cursor.getString(i);
//other stuff
}
} while (cursor.moveToNext());
cursor.close();
}
Run Code Online (Sandbox Code Playgroud)
那么错误意味着什么以及为什么应用程序崩溃?
小智 19
错误-12表示游标泄漏.请尝试关闭它如下:
try {....} finally { cursor.close();}
Run Code Online (Sandbox Code Playgroud)
Android游标将所有查询结果读入内存,并且该数据的限制为1 MB。
选择此限制是因为此数据量可能会使您的应用在移动设备上运行缓慢。
如果可能,您应该:
SELECT *
而仅获取所需的列,并使用WHERE
过滤器);