微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android数据库连接和游标哦我的

在使用android时,我已经阅读了大量有关如何创建和使用数据库连接的博客和教程.虽然我有很多工作示例,但不同的实现会导致不同的问题.

例如,我使用数据源类,数据源和数据库助手类DBManagement.

数据源

public class DataSource {
    // Database fields
    private SQLiteDatabase database;
    private DBManagement dbHelper;

    public SMSDataSource(Context context) {
        dbHelper = new DBManagement(context);
    }

    public void open() throws SQLException {
        if(database == null){
             database = dbHelper.getWritableDatabase();
        }
    }

public Cursor exampleCursor(long constraint){
    Cursor cur = database.query(DBManagement.TABLE_NAME,
            new String[] {DBManagement.Column}, "constraint="+constraint, null, null, null, null);
    return cur;
}
    //.. other methods down here which do rawQuery, QueryBuilder, etc.. 

DBManagement

public class DBManagement extends SQLiteOpenHelper{

    // .. table definitions and columns etc ..//

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

在活动中的onCreate方法中,我将调用datasource.open()并且SQL连接已打开.之后我会做:

DataSource datasource = new DataSource();

Cursor cursor = datasource.exampleCursor(1);
startManagingCursor(cursor);

如果我导航到新活动,我会收到以下错误:

 06-27 21:59:14.812: E/Database(13396): close() was never explicitly called on database '/data/data/com.example.package/databases/db.db' 

如果我添加datasource.close();到onCreate的末尾,没有简单的游标适配器工作,或者如果在conextual菜单上执行操作,我得到db未打开的错误.

处理上述问题的最佳方法是什么?

所以我做了以下,我仍然遇到数据库问题:

@Override
public void onBackPressed() {
    // do something on back.
    //Log.i(getClass().toString(), "onBackPressed");

    datasource.close();

    finish();
    return;
}

@Override
protected void onResume(){
    super.onResume();
    onCreate(null);
}


@Override
protected void onRestart(){
    datasource = new DataSource(this);
    datasource.open();

    filters = datasource.getFilterCursor();
    startManagingCursor(filters);

    super.onRestart();
}

@Override
protected void onPause(){
    //Log.i(getClass().toString(), "onPause");
    ((CursorAdapter) adapter).getCursor().close();
    datasource.close();
    super.onPause();
}   
@Override
protected void onStop(){
    //Log.i(getClass().toString(), "onStop");
    datasource.close();
    super.onStop();
}

我的Datasource.java类具有以下内容:

public Datasource(Context context){
     dbHelper = new DBManagement(context);
}

public void open() throws SQLException {
    if( database == null ){
           database = dbHelper.getWritableDatabase();
    }
}

public void close(){
    if(dbHelper != null){
         dbHelper.close();
    }
}

解决方法:

这其实很简单:

>完成后显式关闭所有游标(最后使用)等.
>不要使用startManagingCursor()
>使你的数据库包装/助手/经理/无论什么类单身
>不要在数据库助手上显式调用close().当您的流程终止时,我将自动关闭.

另外:频繁的打开和关闭绝对是一个坏主意,因为它带来了相当多的开销.

使用装载机也是一种选择.您绝对不需要使用内容提供程序来使用加载程序,但它并不是那么简单.使用内容提供商涉及IPC,如果您不打算将数据导出到其他应用程序,则通常会出现过度杀伤.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐