android database leak found

package com.archermind;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "App.db";
private static final String DATABASE_TABLE = "t_app";
private static final int DATABASE_VERSION = 1;

private final String CREATE_TABLE = "create table if not exists "
    + DATABASE_TABLE + "(id integer not null ,"
    + "Name text not null," + "downUrl text not null,"
    + "state integer not null," + "image blob)";
public DBHelper(Context context) {
   super(context, DATABASE_NAME, null, DATABASE_VERSION);
   // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
   // TODO Auto-generated method stub
   db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO Auto-generated method stub
   String sql = "drop table if exists" + DATABASE_TABLE;
   db.execSQL(sql);
   onCreate(db);
}


/**
* 查询取得结果集

* @return 结果集
*/
public Cursor query() {
   SQLiteDatabase db = this.getReadableDatabase();
   return db.query(DATABASE_TABLE, null, null, null, null, null, null);
}
/**
* 通过Id查询该应用
* @param Id
* @return
* 已存在返回true,否则false
*/
public boolean query(int Id) {
   SQLiteDatabase db = this.getReadableDatabase();
   Cursor cursor = db.query(DATABASE_TABLE, null, "Id = '" + Id
     + "'", null, null, null, null);
   if(cursor.moveToFirst()){
    cursor.close();
    return true;
   }
   cursor.close();
   return false;
}

/**
* 根据Id删除数据

* @param Id
*            应用的id
*/
public void delete(int Id) {
   SQLiteDatabase db = this.getWritableDatabase();
   db.delete(DATABASE_TABLE, "Id = '" + Id + "'", null);
}

/**
* 删除所有的数据
*/
public void delete() {
   SQLiteDatabase db = this.getWritableDatabase();
   db.delete(DATABASE_TABLE, null, null);
}

/**
* 插入数据,

* @param Id
* @param Name
* @param downUrl
* @param state
* @param image
*/
public void insert(int Id, String Name, String downUrl,
    int state, byte[] image) {
   SQLiteDatabase db = this.getWritableDatabase();
   ContentValues values = new ContentValues();
   values.put("Id", Id);
   values.put("Name", Name);
   values.put("downUrl", downUrl);
   values.put("state", state);
   values.put("image", image);
   db.insert(DATABASE_TABLE, null, values);
}
/**
* 通过Id来更新状态
* @param Id
* @param state 
*/
public void update(int appId,int state){
   SQLiteDatabase db = this.getWritableDatabase();
   ContentValues values = new ContentValues();
   values.put("state", state);
   db.update(DATABASE_TABLE, values, "Id = '" + Id + "'", null);
}
}

在操作数据库的时候 DDMS会报出07-05 10:37:19.093: ERROR/Database(4799): Leak found的错误.
就是数据库连接打开后没有关闭的~

//执行sql语句
public void execSQL(String sql){
       SQLiteDatabase db = ...
       db.execSQL(sql);
       db.close();
}

//查询
SQLiteDatabase db = .....
Cursor cur = db.rawQuery(sql, null);
   if(cur!=null){
       if(cur.moveToFirst()){
       do{
       //........                               
            } while(cur.moveToNext());
}
cur.close();        //关闭游标
}
db.close();

你可能感兴趣的:(database)