如何避免数据库不关闭和光标异常 [英] How to avoid db not close and cursor exception

查看:127
本文介绍了如何避免数据库不关闭和光标异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  02-02 14:31:34.048:WARN / SQLiteCompiledSql(359):在终结释放语句。请确保您显式调用close()在你的光标:SELECT * FROM
02-02 14:31:34.048:WARN / SQLiteCompiledSql(359):android.database.sqlite.DatabaseObjectNotClosedException:应用程序没有关闭在这里打开的游标或数据库对象
02-02 14:31:34.129:ERROR /数据库(359):android.database.sqlite.DatabaseObjectNotClosedException:应用程序没有关闭在这里打开的游标或数据库对象
02-02 14:31:34.129:ERROR /数据库(359):在
 

如何避免这种例外?请帮助

我的code为如下:

  ** DataSQLHelper的.class **

公共类DataSQLHelper扩展SQLiteOpenHelper {
私有静态最后弦乐DATABASE_NAME =test.db的;
私有静态最终诠释DATABASE_VERSION = 1;

   公共DataSQLHelper(上下文的背景下){
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
}



@覆盖
公共无效的onCreate(SQLiteDatabase DB){
    字符串SQL =创建表+表+(+ BaseColumns._ID
    +整数主键自动增量,+编号+文本
    +密码+文本,+主动+文本,+状态
    +文本);;
    db.execSQL(SQL);

}

@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
如果(oldVersion> =静态网页)
    返回;

字符串SQL = NULL;
如果(oldVersion == 1)
    SQL =ALTER TABLE+表+添加注释文本;;
如果(oldVersion == 2)
    SQL =;


如果(SQL!= NULL)
    db.execSQL(SQL);
}

        @覆盖
    市民同步无效的close(){
                   super.close();


    }

}


    *** Test_Java的.java ***

     公共类Test_Java延伸活动{
  DataSQLHelper helData;
SQLiteDatabase分贝;
光标光标;


@覆盖
公共无效的onCreate(包savedInstanceState){

super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    尝试 {

        helData =新DataSQLHelper(本);
     光标=的getData();
    startManagingCursor(光标);

    的setContentView(R.layout.view);


}赶上(例外前){

    }

    } //的onCreate完



私人光标的getData(){
尝试 {
     DB = helData.getReadableDatabase();
     光标= db.query(DataSQLHelper.TABLE,NULL,NULL,
        NULL,NULL,NULL,NULL);
    startManagingCursor(光标);
    返回游标;
}赶上(例外前){
    的System.out.println(异常出现:+ ex.toString());
    返回null;
}

}

@覆盖
保护无效的onDestroy(){
   的System.out.println(的onDestroy);
    super.onDestroy();
    如果(DB!= NULL){
                   db.close();
                }
    如果(光标!= NULL){
                    cursor.close();
    }

    如果(helData!= NULL){
        helData.close();
    }

}
}
 

解决方案

我解决了异常。我打电话

  DB = eventsData.getReadableDatabase();
 

两次,这就是为什么抛出异常

02-02 14:31:34.048: WARN/SQLiteCompiledSql(359): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT * FROM 
02-02 14:31:34.048: WARN/SQLiteCompiledSql(359): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-02 14:31:34.129: ERROR/Database(359): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-02 14:31:34.129: ERROR/Database(359):     at 

How to avoid this exception ?? Pls help

My code is given below :

 **DataSQLHelper .class**

public class DataSQLHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "test.db";
private static final int DATABASE_VERSION = 1;

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



@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "create table " + TABLE + "( " + BaseColumns._ID
    + " integer primary key autoincrement, " + ID + " text, "
    + PASSWORD + " text, " + ACTIVE + " text, " + STATUS
    + " text);";
    db.execSQL(sql);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >= newVersion)
    return;

String sql = null;
if (oldVersion == 1)
    sql = "alter table " + TABLE + " add note text;";
if (oldVersion == 2)
    sql = "";


if (sql != null)
    db.execSQL(sql);
}

        @Override
    public synchronized void close() {
                   super.close();


    }

}


    ***Test_Java .java***

     public class Test_Java extends Activity {
  DataSQLHelper helData;
SQLiteDatabase db;
Cursor cursor;


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {      

        helData= new DataSQLHelper(this);
     cursor = getData();
    startManagingCursor(cursor);

    setContentView(R.layout.view);


} catch (Exception ex) {

    }

    } // onCreate Ends



private Cursor getData() {
try {
     db = helData.getReadableDatabase();
     cursor = db.query(DataSQLHelper.TABLE, null, null,
        null, null, null, null);
    startManagingCursor(cursor);
    return cursor;
} catch (Exception ex) {
    System.out.println("Exception Occured : " + ex.toString());
    return null;
}

}

@Override
protected void onDestroy() {
   System.out.println("onDestroy");
    super.onDestroy();
    if (db!=null){
                   db.close();
                }
    if (cursor!=null){
                    cursor.close();
    }

    if ( helData!=null){
        helData.close();
    }

}
}

解决方案

I resolved the exception. I was calling

 db = eventsData.getReadableDatabase();

twice thats why the exception is thrown

这篇关于如何避免数据库不关闭和光标异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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