English 中文(简体)
数据库未关闭光标。 在插入方法之后关闭它
原标题:Database unclosed cursor. Close it after insert method

Would you mind pointing out what s wrong with this peace of code. genresCursor contains "Application did not close the cursor or database object that was opened here" exception. How do I really close this cursor after inserting ? Thanks.

UPD: 似乎完全没有问题。 尽管它包含了仍然可以提取数据的例外。 我实际上的应用程序中一定有错误, 而这个例外让我得出结论,那就是问题所在。 感谢大家的参加。

public class DatabaseCursorActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        HashMap<Integer, String> _dummy = new HashMap<Integer, String>();

        OpenDatabaseHelper helper = new OpenDatabaseHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(OpenDatabaseHelper.GENRES_ID_KEY, 1);
        values.put(OpenDatabaseHelper.GENRES_TITLE_KEY, "Test");
        db.insert(OpenDatabaseHelper.GENRES_TABLE_NAME, null, values);
        db.close();
        helper.close();

        db = helper.getReadableDatabase();
        Cursor genresCursor = db.query(OpenDatabaseHelper.GENRES_TABLE_NAME, new String[]{OpenDatabaseHelper.GENRES_ID_KEY, OpenDatabaseHelper.GENRES_TITLE_KEY }, null, null, null, null, null);
        int i = genresCursor.getColumnCount();
        genresCursor.moveToFirst();
    }

    public class OpenDatabaseHelper extends SQLiteOpenHelper {

        public static final String GENRES_TABLE_NAME = "genres";
        public static final String GENRES_ID_KEY = "id";
        public static final String GENRES_TITLE_KEY = "title";

        public OpenDatabaseHelper(Context context) {
            super(context, "ttt.db", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + GENRES_TABLE_NAME + "( id integer primary key not null, title text);" );
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }

    }

}
问题回答

您先打开数据库,然后插入后关闭数据库,然后再次打开以读取值。

首先,你不需要再关闭和打开它。 你可以做你所有的操作, 并且可以关闭它,因为你仍然在相同的代码块。

在用您的genresCursor 完成操作后, 由genresCursor. close () 关闭它, 然后关闭您的 db. 希望它会好起来 。

我不明白你为什么加入这个代码:

db = helper.getReadableDatabase();
Cursor genresCursor = db.query(OpenDatabaseHelper.GENRES_TABLE_NAME, new String[]{OpenDatabaseHelper.GENRES_ID_KEY, OpenDatabaseHelper.GENRES_TITLE_KEY }, null, null, null, null, null);
int i = genresCursor.getColumnCount();
genresCursor.moveToFirst();

问题在于代码的这一部分。 如果您想要关闭光标, 请调用 genresCursor. close > () 。 但是从结构角度我不明白为什么您需要这个代码 。

光标和数据库工作的最佳实践是您应该打开它, 并关闭在相同的代码块中。 当我在数据库中插入行时, 我用它来开始打开它, 然后插入许多列, 然后在返回前关闭它 。

在 Honeycomb 之前的 API 中, 您不会收到任何未关闭光标的日志消息和性能问题, 但是在 Honeycomb 之前的 API 没有关闭的光标光标的光标光标记录信息, 但它没有关闭的光标检测器 。

Edit: Close Database within same block of code. Cursor can be closed as per the scenario but must be closed before Context Deletion.





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签