具有SQLite的問題,我,我不知道我可以解決Android的SQLite數據庫泄露錯誤
我不知道這個錯誤可以使OutOfMemoryError異常,因爲我有這個錯誤太所以請幫助我解決這個錯誤
A SQLiteConnection object for database ......... was leaked!
我的數據庫代碼
public class DB_Sqlit extends SQLiteOpenHelper {
public static final String BDname = "mdata.db";
public DB_Sqlit(Context context) {
super(context, BDname, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table favorite (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, img TEXT, url TEXT, num TEXT, size TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS favorite");
onCreate(db);
}
public Boolean Insert_to_favorite(String name, String img, String url, String num, String size) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("img", img);
contentValues.put("url", url);
contentValues.put("num", num);
contentValues.put("size", size);
long result = db.insert("favorite", null, contentValues);
if (result == -1)
return false;
else
return true;
}
public List getAllList_Favorite() {
List<listitem_gib> list = new ArrayList<listitem_gib>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor rs = db.rawQuery("select * from favorite", null);
if (rs.moveToFirst()){
do {
listitem_gib model = new listitem_gib();
model.setId(rs.getString(0));
model.setName(rs.getString(1));
model.seturl(rs.getString(3));
model.setimg(rs.getString(2));
model.setnum(rs.getString(4));
model.setsize(rs.getString(5));
list.add(model);
}while (rs.moveToNext());
}
Log.d("rewayat data", list.toString());
return list;
}
public int get_check_List_Favorite(String Title) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor rs = db.rawQuery("select * from favorite Where name like '"+ Title +"'", null);
rs.moveToFirst();
Cursor cursor = null;
try {
int count = rs.getCount();
return count;
// do some work with the cursor here.
} finally {
// this gets called even if there is an exception somewhere above
if(cursor != null)
cursor.close();
}
}
public Integer DeleteFav(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("favorite", "id = ?", new String[]{id});
}}}
如何IC請修復此錯誤?
我有問題,這個錯誤可以使OutOfMemoryError?
數據庫編輯
public class DB_Sqlit extends SQLiteOpenHelper {
public static final String BDname = "mdata.db";
public DB_Sqlit(Context context) {
super(context, BDname, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table favorite (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, img TEXT, url TEXT, num TEXT, size TEXT)");
db.execSQL("create table download (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, img TEXT, url TEXT, num TEXT, size TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS favorite");
db.execSQL("DROP TABLE IF EXISTS download");
onCreate(db);
}
public Boolean Insert_to_favorite(String name, String img, String url, String num, String size) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("img", img);
contentValues.put("url", url);
contentValues.put("num", num);
contentValues.put("size", size);
long result = db.insert("favorite", null, contentValues);
db.close();
if (result == -1)
return false;
else
return true;
}
public List getAllList_Favorite() {
List<listitem_gib> list = new ArrayList<listitem_gib>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor rs = db.rawQuery("select * from favorite", null);
if (rs.moveToFirst()){
do {
listitem_gib model = new listitem_gib();
model.setId(rs.getString(0));
model.setName(rs.getString(1));
model.seturl(rs.getString(3));
model.setimg(rs.getString(2));
model.setnum(rs.getString(4));
model.setsize(rs.getString(5));
list.add(model);
}while (rs.moveToNext());
}
Log.d("rewayat data", list.toString());
rs.close();
db.close();
return list;
}
public int get_check_List_Favorite(String Title) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor rs = db.rawQuery("select * from favorite Where name like '"+ Title +"'", null);
rs.moveToFirst();
Cursor cursor = null;
try {
int count = rs.getCount();
return count;
// do some work with the cursor here.
} finally {
// this gets called even if there is an exception somewhere above
if(cursor != null){
rs.close();
db.close();
}
}
}
public Integer DeleteFav(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("favorite", "id = ?", new String[]{id});
}
public Boolean Insert_to_download(String name, String img, String url, String num, String size) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("img", img);
contentValues.put("url", url);
contentValues.put("num", num);
contentValues.put("size", size);
long result = db.insert("download", null, contentValues);
db.close();
if (result == -1)
return false;
else
return true;
}
public List getAllList_download() {
List<listitem_gib> list = new ArrayList<listitem_gib>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor rs = db.rawQuery("select * from download", null);
if (rs.moveToFirst()){
do {
listitem_gib model = new listitem_gib();
model.setId(rs.getString(0));
model.setName(rs.getString(1));
model.seturl(rs.getString(3));
model.setimg(rs.getString(2));
model.setnum(rs.getString(4));
model.setsize(rs.getString(5));
list.add(model);
}while (rs.moveToNext());
}
Log.d("rewayat data", list.toString());
rs.close();
db.close();
return list;
}
public int get_check_List_download(String Title) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor rs = db.rawQuery("select * from download Where name like '"+ Title +"'", null);
rs.moveToFirst();
Cursor cursor = null;
try {
int count = rs.getCount();
return count;
// do some work with the cursor here.
} finally {
// this gets called even if there is an exception somewhere above
if(cursor != null) {
rs.close();
db.close();
}
}
}
public Integer Deletedown(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("download", "id = ?", new String[]{id});
}}
你需要關閉你的遊標每次你得到它們 –
你能告訴我我是如何新的Android和我從教程 – fekra
看到這個http://stackoverflow.com/questions/12801602/android-sqlite-泄露 –