Android的数据库连接和游标噢,我的 [英] Android database connections and cursors oh my

查看:143
本文介绍了Android的数据库连接和游标噢,我的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了很多博客和教程就如何创建和使用Android时使用的数据库连接。虽然我有很多工作的例子,不同的实现会导致不同的问题。

比如,我使用的数据源类,数据源和数据库辅助类, DBManagement

数据源

 公共类数据源{
    //数据库字段
    私人SQLiteDatabase数据库;
    私人DBManagement dbHelper;    公共SMSDataSource(上下文的背景下){
        dbHelper =新DBManagement(背景);
    }    公共无效的open()抛出的SQLException {
        如果(数据库== NULL){
             数据库= dbHelper.getWritableDatabase();
        }
    }公共光标exampleCursor(长约束){
    光标CUR = database.query(DBManagement.TABLE_NAME,
            新的String [] {} DBManagement.Column,约束=+约束,NULL,NULL,NULL,NULL);
    返回CUR;
}
    // ..其它方法到这里里面做rawQuery,QueryBuilder的,等等。

DBManagement

 公共类DBManagement扩展SQLiteOpenHelper {    // ..表定义和列等..//    公共DBManagement(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
    }

在我的活动中的onCreate方法,我称之为 datasource.open()和SQL连接打开。之后,我会做什么:

 数据源的数据源=新的DataSource();光标的光标= datasource.exampleCursor(1);
startManagingCursor(光标);

如果我浏览到一个新的活动,我得到以下错误:

  06-27 21:59:14.812:E /数据库(13396):关闭()从未被明确要求在数据库'/data/data/com.example.package/databases /db.db

如果我添加 datasource.close(); 来的onCreate结束后,没有一个简单的游标适配器的工作,或者说,数据库是不打开,如果我得到的错误一个conextual菜单上执行的操作。

什么是处理上述的最佳方式?

所以,我做以下,我仍然得到数据库的问题:

  @覆盖
公共无效onBack pressed(){
    //做回事情。
    //Log.i(getClass()的toString(),onBack pressed);    datasource.close();    完();
    返回;
}@覆盖
保护无效onResume(){
    super.onResume();
    的onCreate(NULL);
}
@覆盖
保护无效onRestart(){
    数据源=新的DataSource(本);
    datasource.open();    过滤器= datasource.getFilterCursor();
    startManagingCursor(过滤器);    super.onRestart();
}@覆盖
保护无效的onPause(){
    //Log.i(getClass()的toString()的onPause);
    ((CursorAdapter的)适配器).getCursor()close()方法;
    datasource.close();
    super.onPause();
}
@覆盖
保护无效的onStop(){
    //Log.i(getClass()的toString()的onStop);
    datasource.close();
    super.onStop();
}

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

 公共数据源(上下文的背景下){
     dbHelper =新DBManagement(背景);
}公共无效的open()抛出的SQLException {
    如果(数据库== NULL){
           数据库= dbHelper.getWritableDatabase();
    }
}公共无效的close(){
    如果(dbHelper!= NULL){
         dbHelper.close();
    }
}


解决方案

这其实很简单:


  • 明确关闭所有的光标的完成时(使用最后)等。

  • 请不要使用 startManagingCursor()

  • 请你的数据库封装/帮手/经理/不管类单身

  • 请不要显式调用的close()上的数据库帮手的。我会当你的进程终止自动关闭。

此外:频繁的开幕式和闭幕式绝对是一个坏主意,因为它会带来不少的开销。

使用装载机也是一种选择。你绝对不需要使用内容提供商使用装载机但它是没有那么简单,因为它可以。使用内容提供商包括IPC通常是矫枉过正,如果你不打算将数据导出到其他应用程序。

I have read plenty of blogs and tutorials on how to create and use database connections when working with android. Although I have plenty of working examples, different implementations result in different issues.

Example, I use a datasource class, Datasource and a database helper class, DBManagement.

DataSource

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);      
    }

In my onCreate methods within activity, I will call datasource.open() and the SQL connection is open. After that I will do:

DataSource datasource = new DataSource();

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

If I navigate to a new activity, I get the following error:

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

If i add datasource.close(); to the end of onCreate, none of the simple cursor adapters work, or I get errors that the db is not open if perform an action on a conextual menu.

What is the best way to handle the above?

So I did the following, and I am still getting the database issue:

@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();
}

My Datasource.java class has the following:

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();
    }
}

解决方案

This is actually very simple:

  • explicitly close all your cursors when done (using finally), etc.
  • don't use startManagingCursor()
  • make your database wrapper/helper/manager/whatever class a singleton
  • don't explicitly call close() on the database helper. I will be automatically closed when your process dies.

Additionally: frequent opening and closing is definitely a bad idea, since it carries quite a lot of overhead.

Using loaders is also an option. You definitely don't need to use a content provider to use a loader however it is not as straightforward as it could be. Using an content provider involves IPC which is usually overkill if you don't plan to export your data to other apps.

这篇关于Android的数据库连接和游标噢,我的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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