android sqlite数据库访问引发锁定错误

时间:2013-09-11 10:34:14

标签: android sqlite

我有一个Android服务,我试图访问一个sqlite数据库。代码运行正常,但我无法访问数据库。我认为错误与数据库锁有关。 以下是我访问数据库的方法:

private void checkWatchList() {
    Log.i("watch list", "watch list started");
    Cursor cursor;
    try {
        cursor = dbhandler.getAlerts();
        if (cursor.moveToFirst()) {
            while (cursor.moveToNext()) {
                Log.i("watch list", "checked");
                String share_name = cursor.getString(cursor
                        .getColumnIndex(KEY_SHARE_NAME));
                String max_price = cursor.getString(cursor
                        .getColumnIndex(KEY_MAX_PRICE));
                String min_price = cursor.getString(cursor
                        .getColumnIndex(KEY_MIN_PRICE));
                int action = cursor.getInt(cursor
                        .getColumnIndex(KEY_ACTION));

                if (min_price.equals("NULL")) {
                    double maxprice = Double.parseDouble(max_price);
                    compareMax(share_name, maxprice, action);
                } else if (max_price.equals("NULL")) {
                    double minprice = Double.parseDouble(min_price);
                    compareMin(share_name, minprice, action);
                } else {
                    double maxprice = Double.parseDouble(max_price);
                    double minprice = Double.parseDouble(min_price);
                    compareMinMax(share_name, minprice, maxprice, action);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void compareMin(String sharename, double minprice, int action) {
    Log.i("compare min", "compare min");
    Cursor cursor;
    double new_price;
    String message = "The price is below minimum set price";
    try {
        cursor = dbhandler.compare(sharename);
        new_price = cursor.getDouble(cursor.getColumnIndex(KEY_PRICE));

        if (minprice <= new_price) {
            dbhandler.addTask(sharename, action, message);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private void compareMinMax(String sharename, double minprice,
        double maxprice, int action) {
    Log.i("compare minmax", "compare minmax");
    Cursor cursor;
    String message = "This price is below minimum set price.";
    try {
        cursor = dbhandler.compare(sharename);
        double new_price = cursor.getDouble(cursor
                .getColumnIndex(KEY_PRICE));
        if (new_price <= minprice) {
            dbhandler.addTask(sharename, action, message);
        } else if (new_price >= maxprice) {
            dbhandler.addTask(sharename, action, message);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void compareMax(String sharename, double maxprice, int action) {
    Log.i("compare max", "compare max");
    Cursor cursor;
    String message = "This price is more than maximum set price.";
    try {
        cursor = dbhandler.compare(sharename);
        double new_price = cursor.getDouble(cursor
                .getColumnIndex(KEY_PRICE));

        if (new_price >= maxprice) {
            dbhandler.addTask(sharename, action, message);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

以下是DatabaseHandler clas中的方法:

public Cursor getAlerts() {
    Cursor cursor;
    SQLiteDatabase database = this.getReadableDatabase();
    cursor = database.query(TABLE_ALERTS, new String[] { KEY_SHARE_NAME,
            KEY_MIN_PRICE, KEY_MAX_PRICE, KEY_ACTION }, null, null, null,
            null, null);
    database.close();
    return cursor;
}

public void addTask(String name, int action, String message) {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_SHARENAME, name);
    values.put(KEY_TASK, action);
    values.put(KEY_MESSAGE, message);

    database.insert(TABLE_TASKS, null, values);
    database.close();
}

public Cursor compare(String sharename) {
    Cursor cursor;
    SQLiteDatabase database = this.getReadableDatabase();
    cursor = database.query(TABLE_SHARES, new String[] { KEY_PRICE },
            KEY_NAME + "=?", new String[] { sharename }, null, null, null);
    database.close();
    return cursor;
}

从logcat的输出中,我的代码一直运行到checkWatchList方法,但是没有在循环中执行if条件。这是logcat输出

09-11 13:11:15.289: I/watch list(22091): watch list started
09-11 13:11:15.319: W/System.err(22091): java.lang.IllegalStateException: database /data/data/com.intelligent.stocktrader/databases/stocktrader.db (conn# 0) already closed
09-11 13:11:15.329: W/System.err(22091):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
09-11 13:11:15.329: W/System.err(22091):    at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:413)
09-11 13:11:15.329: W/System.err(22091):    at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:400)
09-11 13:11:15.329: W/System.err(22091):    at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:79)
09-11 13:11:15.339: W/System.err(22091):    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:160)
09-11 13:11:15.339: W/System.err(22091):    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:152)
09-11 13:11:15.339: W/System.err(22091):    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:158)
09-11 13:11:15.349: W/System.err(22091):    at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:235)
09-11 13:11:15.349: W/System.err(22091):    at com.intelligent.stocktrader.database.ShareService.checkWatchList(ShareService.java:160)
09-11 13:11:15.349: W/System.err(22091):    at com.intelligent.stocktrader.database.ShareService.onCreate(ShareService.java:63)
09-11 13:11:15.359: W/System.err(22091):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2252)
09-11 13:11:15.359: W/System.err(22091):    at android.app.ActivityThread.access$1600(ActivityThread.java:122)
09-11 13:11:15.359: W/System.err(22091):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
09-11 13:11:15.369: W/System.err(22091):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 13:11:15.369: W/System.err(22091):    at android.os.Looper.loop(Looper.java:137)
09-11 13:11:15.369: W/System.err(22091):    at android.app.ActivityThread.main(ActivityThread.java:4340)
09-11 13:11:15.369: W/System.err(22091):    at java.lang.reflect.Method.invokeNative(Native Method)
09-11 13:11:15.379: W/System.err(22091):    at java.lang.reflect.Method.invoke(Method.java:511)
09-11 13:11:15.379: W/System.err(22091):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-11 13:11:15.379: W/System.err(22091):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-11 13:11:15.379: W/System.err(22091):    at dalvik.system.NativeStart.main(Native Method)
09-11 13:11:15.389: I/LocalService(22091): Received start id 1: Intent { cmp=com.intelligent.stocktrader/.database.ShareService }

2 个答案:

答案 0 :(得分:0)

我可以在这里复制粘贴我的部分代码:)。希望它有所帮助。

        public zipAreas getzipArea(int zipAreaId) {

    SQLiteDatabase db = _dbhelper.getReadableDatabase();
    Cursor c = null;
    try {
        zipAreas zipArea = null;
        String[] selectionArgs = new String[1];
        selectionArgs[0] = Integer.toString(zipAreaId);

        c = db.rawQuery("select * from " + TABLE_zipAreaS + " where "
                + COLUMN_ID + " = ?", selectionArgs);

        if (c.getCount() > 0) {
            c.moveToFirst();

            zipArea = new zipAreas(c.getInt(c.getColumnIndex(COLUMN_ID)),
                    c.getString(c.getColumnIndex(COLUMN_BIZ_AREA)));
        }

        return zipArea;
    } catch (Exception e) {

        CoreDataManager.handleException(new DatabaseException(e));
        return null;
    } finally {
        if (null != c)
            c.close();
    }
}

答案 1 :(得分:0)

delete database.close();在你的方法getAlerts和compare(String str)

 public Cursor getAlerts() {
        Cursor cursor;
        SQLiteDatabase database = this.getReadableDatabase();
        cursor = database.query(TABLE_ALERTS, new String[] { KEY_SHARE_NAME,
                KEY_MIN_PRICE, KEY_MAX_PRICE, KEY_ACTION }, null, null, null,
                null, null);
        return cursor;
    }

    public void addTask(String name, int action, String message) {
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_SHARENAME, name);
        values.put(KEY_TASK, action);
        values.put(KEY_MESSAGE, message);

        database.insert(TABLE_TASKS, null, values);
        database.close();
    }

    public Cursor compare(String sharename) {
        Cursor cursor;
        SQLiteDatabase database = this.getReadableDatabase();
        cursor = database.query(TABLE_SHARES, new String[] { KEY_PRICE },
                KEY_NAME + "=?", new String[] { sharename }, null, null, null);
        return cursor;
    }

并查看此http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/