数据库未关闭光标。插入方法后关闭它 [英] Database unclosed cursor. Close it after insert method

查看:209
本文介绍了数据库未关闭光标。插入方法后关闭它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你介意指出有什么不对的和平code的。 genresCursor包含应用程序没有关闭在这里打开游标或数据库对象异常。如何我真的关闭这个游标插入后?
谢谢你。

UPD:好像没有一个问题都没有。尽管它包含不同的是仍然可以提取数据。我一定是错在我的实际应用和这个异常导致我的结论就是它一直是问题。谢谢大家参与。

 公共类DatabaseCursorActivity延伸活动{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        HashMap的<整数,字符串> _dummy =新的HashMap<整数,字符串>();        OpenDatabaseHelper帮手=新OpenDatabaseHelper(本);
        SQLiteDatabase分贝= helper.getWritableDatabase();
        ContentValues​​值=新ContentValues​​();
        values​​.put(OpenDatabaseHelper.GENRES_ID_KEY,1);
        values​​.put(OpenDatabaseHelper.GENRES_TITLE_KEY,测试);
        db.insert(OpenDatabaseHelper.GENRES_TABLE_NAME,空,价值);
        db.close();
        helper.close();        DB = helper.getReadableDatabase();
        光标genresCursor = db.query(OpenDatabaseHelper.GENRES_TABLE_NAME,新的String [] {OpenDatabaseHelper.GENRES_ID_KEY,OpenDatabaseHelper.GENRES_TITLE_KEY},NULL,NULL,NULL,NULL,NULL);
        INT I = genresCursor.getColumnCount();
        genresCursor.moveToFirst();
    }    公共类OpenDatabaseHelper扩展SQLiteOpenHelper {        公共静态最后弦乐GENRES_TABLE_NAME =流派;
        公共静态最后弦乐GENRES_ID_KEY =ID;
        公共静态最后弦乐GENRES_TITLE_KEY =称号;        公共OpenDatabaseHelper(上下文的背景下){
            超级(上下文中,ttt.db,空,1);
        }        @覆盖
        公共无效的onCreate(SQLiteDatabase DB){
            db.execSQL(CREATE TABLE+ GENRES_TABLE_NAME +(ID整数主键不为空,标题文字););
        }        @覆盖
        公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){        }    }}


解决方案

光标和数据库工作最好的做法是,你应该打开它,在code的同一块将其关闭。当我使用将行插入数据库,我用在开始将其打开,然后插入多列,只是在返回前关闭它。

在蜂窝API之前,你不会得到关于未关闭的游标,但API的任何日志消息和性能问题,蜂窝体的游标未封闭的检测器,它会记录信息有关游标没有被关闭之前。

编辑:
code的同一个街区之内关闭数据库。游标可以关闭根据该方案,但必须Context删除前关闭。

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: It seems like there wasn't a problem at all. Even though it contains exception that's still possible to extract data. I must've been wrong in my real application and this exception led me to conclusion that's it had been the issue. Thanks everyone for participating.

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) {

        }

    }

}

解决方案

The Best Practice to work with cursor and database is that you should open it and close it in the same block of code. When I use to insert rows into database I use to open it at start and then insert many column and close it just before returning.

In API before Honeycomb you wont get any log message and performance issue regarding unclosed cursor but API before Honeycomb has a Cursor not closed detector which will log message about cursor not being closed.

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

这篇关于数据库未关闭光标。插入方法后关闭它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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