从未在数据库上显式调用close()

时间:2011-09-15 00:29:03

标签: android sqlite broadcastreceiver

当收到sms时,我需要将sms存储在sqlite数据库中。 我正在使用这些课程:

DBHelper.java

public class DBHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "my_db";
    private static final int DB_VERSION = 1;

    public DBHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, DB_NAME, factory, DB_VERSION);
    }

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public String getName(){
        return DB_NAME;
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql="";
        sql+="CREATE TABLE sms ( ";
            sql+="_id INTEGER PRIMARY KEY AUTOINCREMENT, ";
        sql+="mit TEXT NOT NULL, ";
        sql+="dest TEXT NOT NULL, ";
        sql+="text TEXT NOT NULL, ";
        sql+="date DATETIME NOT NULL )";

        db.execSQL(sql);
    }

DBManager.java

public class DBManager {

private DBHelper dbh;
SQLiteDatabase dbr;
SQLiteDatabase dbw;

DBManager(DBHelper dbh){
    this.dbh=dbh;
    dbr=dbh.getReadableDatabase(); //line 24
    dbw=dbh.getWritableDatabase();
}

DBManager(Context ctx){
    this.dbh=new DBHelper(ctx);
    dbr=dbh.getReadableDatabase();
    dbw=dbh.getWritableDatabase();
}

public long insert(String table, ContentValues values){
    return dbw.insert(table, null, values);
}

public long insertSMS(Sms sms){
    if(sms.getSender().length()>0 && sms.getReceiver().length()>0 && sms.getText()!=null){
    String sql="INSERT INTO sms VALUES ( null, \""+sms.getSender()+"\", \""+sms.getReceiver()+"\", \""+sms.getText()+"\", datetime())";
    dbw.execSQL(sql);
    return 0;
    }
    return -1;
}

public Sms getSmsById(int id){
    String[] columns = { "mit","dest","text","date" }, selectionArgs={ Integer.toString(id) };
    String selection="_id = ?";
    Cursor c=dbr.query("sms", columns, selection, selectionArgs, null, null, "_id ASC");
    if(c.moveToFirst())
    return new Sms(c.getString(0),c.getString(1), c.getString(2),c.getString(3));
    return null;
}

public Sms[] getAllSms(){
        String[] columns = { "mittente","destinatario","testo","dataora" };
        Cursor c=dbr.query("sms", columns, null, null, null, null, "_id ASC");
        ArrayList<Sms> al=new ArrayList<Sms>();
        if(c.moveToFirst())
        do{
            al.add(new Sms(c.getString(0),c.getString(1),c.getString(2),c.getString(3)));
        }while(c.moveToNext());
        return (Sms[]) al.toArray();
    }

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

        if(dbw!=null)
        dbw.close();

        if(dbr!=null)
        dbr.close();
    }}

SmsBR.java

public class SmsBR extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {               
                    Bundle bundle = intent.getExtras();
                    if (bundle != null) {
                        Object[] pdus = (Object[])bundle.get("pdus");
                        final SmsMessage[] messages = new SmsMessage[pdus.length];
                        for (int i = 0; i < pdus.length; i++) {
                            messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        }
                        if (messages.length > 0) {
                            System.out.println("I received an sms from: "+messages[0].getOriginatingAddress());
                            DBManager dbm = new DBManager(context);
                            dbm.insertSMS(messages[0]);
                            dbm.close();
                        }}}}

但是我收到了这个错误:

close() was never explicitly called on database '/data/data/it.giox.sms/databases/my_db' 
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:549)
     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
     at it.giox.sms.DBManager.<init>(DBManager.java:24)
     at it.giox.sms.DroidClient.<init>(DroidClient.java:29)
     at it.giox.sms.DroidWebServer.getClient(DroidWebServer.java:25)
     at it.giox.sms.WebServer.startServer(WebServer.java:101)
     at it.giox.sms.WebServer.run(WebServer.java:60)
     at java.lang.Thread.run(Thread.java:1019)

我可以改变什么?

[编辑] 我已完成此修正,但错误仍然存​​在......

public Sms[] getAllSms(){
        String[] columns = { "mittente","destinatario","testo","dataora" };
        ArrayList<Sms> al=new ArrayList<Sms>(); Cursor c = null;
        try{
        c=dbr.query("sms", columns, null, null, null, null, "_id ASC");
                if(c.moveToFirst())
        do{
            al.add(new Sms(c.getString(0),c.getString(1),c.getString(2),c.getString(3)));
        }while(c.moveToNext());}
        finally{
        c.close();
        close();
        }

        return (Sms[]) al.toArray();
        }

[编辑2]

问题是在DBManager构造函数中我创建了一个new SQLiteDatabase然后我应该close()它。如果我在构造函数中放置close()我没有错误,但系统告诉我数据库已关闭。 我解决了每次需要时创建SQLiteDatabase的新实例的问题,最后关闭它。它有效,但我认为有更好的解决方案!

public Sms[] getAllSms(){
        SQLiteDatabase dbw;
                ...
                try{
                dbw=dbh.getWritableDatabase();
                ...
                } finally {dbw.close();}}

1 个答案:

答案 0 :(得分:2)

完成游标后,您需要关闭游标。在try/finallygetAllSms()中使用getSmsById()可在读取数据后关闭光标。