光标窗口分配2048 kb失败.#Open Cursors = 1(此proc = 1打开的#游标)

She*_*rib 7 database android android-cursor android-sqlite android-4.4-kitkat

我正在制作一个使用数据库的Kiosk应用程序.该应用程序一直在前台运行.该应用程序有许多使用一个共享DataBaseHelper实例的线程.该应用程序完美无缺,但大多数时间在5或6小时后,我遇到这些异常,然后应用程序崩溃:

E/DataBaseHelper:2048 kb的游标窗口分配失败.#Open Cursors = 1(此proc = 1打开的#游标)

E/CursorWindow:由于错误-24,无法分配大小为2097152的CursorWindow'/data/user/0/com.kios.frm/databases/YadProjectDB.db'.

E/SQLiteLog:(14)无法打开[00bb9c9ce4]第30192行的文件

E/SQLiteLog:(14)语句中止16:[SELECT number FROM sms LIMIT 5]无法打开数据库文件

E/SQLiteQuery:异常:无法打开数据库文件(代码14);
查询:SELECT编号FROM sms LIMIT 5

E/SQLiteLog:(14)os_unix.c:30192:(24)
open(/data/user/0/com.kiosk.frm/databases/YadProjectDB.db-journal) -

我正确关闭了光标,但仍然获得了这些异常.那些例外是什么?这些例外的原因是什么?

主要活动

private DataBaseHelper db = null; // Global     
Run Code Online (Sandbox Code Playgroud)

MainActivity onCreate方法:

db = new DataBaseHelper(this);

new Thread(new Runnable() {
@Override
public void run() {
    while (threadRunningFlag) {

    Cursor result = null;
    try {

        result = db.getData("SELECT " + DataBaseHelper.SMS_COLUMN_PHONE_NUMBER  + " FROM " + DataBaseHelper.SMS_TABLE_NAME + " LIMIT 5");
        if (result != null && result.getCount() > 0) {
            while (!result.isAfterLast()) {
                String phoneNumber = result.getString(result.getColumnIndex(DataBaseHelper.SMS_COLUMN_PHONE_NUMBER));
                // ...
                result.moveToNext();
            }
        }
    }catch (Exception e) {
        Log.e(TAG, "err->" + e.getLocalizedMessage());

    }finally {
        if (result != null) {
            result.close();
            result = null;
        }
    }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Log.e(TAG, e.getMessage());
        }
    }
}
}).start();
Run Code Online (Sandbox Code Playgroud)

DataBaseHelper类:

public class DataBaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "YadProjectDB.db";
    public static final String SMS_TABLE_NAME = "sms";
    public static final String SMS_COLUMN_PHONE_NUMBER = "number";
    public static final String SMS_COLUMN_SMS_TEXT = "message";

    public static final String BLACK_LIST_TABLE_NAME = "blackList";
    public static final String BLACK_LIST_COLUMN_ID = "id";
    public static final String BLACK_LIST_COLUMN_PHONE_NUMBER = "number";

    private final String TAG = "DataBaseHelper";

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate( SQLiteDatabase db ) {
        String command = "CREATE TABLE "
                + SMS_TABLE_NAME
                + "("
                + SMS_COLUMN_PHONE_NUMBER + " TEXT,"
                + SMS_COLUMN_SMS_TEXT + " TEXT,"
                + ")";
        try {
            db.execSQL(command);
        }catch (Exception e) {
            Log.e(TAG, "err->" + e.getMessage());
        }

        command = "CREATE TABLE "
                + BLACK_LIST_TABLE_NAME
                + "("
                + BLACK_LIST_COLUMN_PHONE_NUMBER + " TEXT,"
                + ")";
        try {
            db.execSQL(command);
        }catch (Exception e) {
            Log.e(TAG, "err->" +  e.getMessage());
        }
    }

    public boolean insertToSms(String number, String message, String fileName, Integer uploadFlag, Integer blackList, Integer pictureFlag)
    {
        final SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(SMS_COLUMN_PHONE_NUMBER, number);
        contentValues.put(SMS_COLUMN_SMS_TEXT, message);
        try {
            db.insert(SMS_TABLE_NAME, null, contentValues);
            return true;
        }catch (Exception e) {
            Log.e(TAG, "err->" + e.getMessage());
            return false;
        }
    }

    public Cursor getData(String query){
        final SQLiteDatabase db = getReadableDatabase();
        Cursor res = null;
        try {
            res = db.rawQuery(query, null);
            res.moveToFirst();
            return res;
        }catch (Exception e) {
            Log.e(TAG, "err->" + e.getMessage());
            if (res != null) {
                res.close();
                res = null;
            }
        }
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ken*_*ndt 9

这看起来与SQLite Android数据库游标窗口分配2048 kb失败的问题相同

你的错误说:"打开游标"

上述问题的答案解释了:

大多数情况下,此错误的原因是非关闭游标.确保在使用后关闭所有游标(即使出现错误).

Cursor cursor = null;
try {
    cursor = db.query(...
    // do some work with the cursor here.
} finally {
    // this gets called even if there is an exception somewhere above
    if(cursor != null)
        cursor.close();
}
Run Code Online (Sandbox Code Playgroud)