我在android中开发简单的应用程序。它执行成功,但不幸的是它停止了一段时间并抛出错误,如这些
数据库SQLiteConnection
的{{1}}对象泄露了!请修复您的应用程序以正确结束正在进行的事务,并在不再需要时关闭数据库。
我该怎么做以避免上述错误
这是我的db代码:
'/data/data/com.h2o/databases/MyDBName.db'
AddressFragment.java:
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String ADDRESS_TABLE_NAME = "address";
public static final String ADDRESS_COLUMN_ID = "id";
public static final String ADDRESS_COLUMN_ADDRESSTYPE = "addrtype";
public static final String ADDRESS_COLUMN_LINE1 = "line1";
public static final String ADDRESS_COLUMN_LINE2 = "line2";
public static final String ADDRESS_COLUMN_CITY = "city";
public static final String ADDRESS_COLUMN_ZIPCODE = "zipcode";
public static final String ADDRESS_COLUMN_COUNTRY = "country";
public static final String ADDRESS_COLUMN_STATE = "state";
public static final String ADDRESS_COLUMN_LANDMARK = "landmark";
//private HashMap hp;
public DBHelper(Context context)
{
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table address " +
"(id integer primary key autoincrement,addrtype text not null unique, line1 text not null,line2 text not null,city text not null,zipcode text not null, country textnot null,state text not null,landmark text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS address");
onCreate(db);
}
public boolean insertAddress(String addrtype, String line1, String line2, String city, String zipcode, String country, String state, String landmark)
{
SQLiteDatabase db;
db= this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("addrtype", addrtype);
contentValues.put("line1", line1);
contentValues.put("line2", line2);
contentValues.put("city", city);
contentValues.put("zipcode", zipcode);
contentValues.put("country", country);
contentValues.put("state", state);
contentValues.put("landmark", landmark);
String selectQuery = "select * from address where addrtype ='"+addrtype+"'";
Cursor cursor = db.rawQuery(selectQuery,null);
if (cursor.getCount()==1){
db.update("address",contentValues,"addrtype ='"+addrtype+"'",null);
return true;
}
else {
db.insert("address", null, contentValues);
return true;
}
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from address where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, ADDRESS_TABLE_NAME);
return numRows;
}
public boolean updateAddress (Integer id,String addrtype, String line1, String line2, String city, String zipcode,String country,String state,String landmark)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("addrtype", addrtype);
contentValues.put("line1", line1);
contentValues.put("line2", line2);
contentValues.put("city", city);
contentValues.put("zipcode", zipcode);
contentValues.put("country", country);
contentValues.put("state", state);
contentValues.put("landmark", landmark);
db.update("address", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteAddress (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("address",
"id = ? ", new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllAddress()
{
ArrayList<String> array_list = new ArrayList<String>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from address", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(ADDRESS_COLUMN_ADDRESSTYPE)));
res.moveToNext();
}
return array_list;
}
}
请有人解决它!
提前致谢
答案 0 :(得分:0)
我认为您收到此错误是因为您没有关闭&#39;光标&#39;宾语。 完成后,每个光标应该关闭。安全的方法是:
Cursor res = db.rawQuery( "select * from address", null);
try {
// read data from the cursor in here
} finally {
res.close();
}
答案 1 :(得分:0)
我是这样用的:
@Override
protected void onDestroy () {
super.onDestroy();
//myDb is Database Helper Class
if(myDb.getReadableDatabase().isOpen())myDb.getReadableDatabase().close();
if(myDb.getWritableDatabase().isOpen())myDb.getWritableDatabase().close();
}