Android SQLite数据库泄露错误

时间:2016-12-01 21:50:05

标签: android sqlite

我遇到了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});
}}}

我怎么能解决这个错误?

我有疑问这个错误可以使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});
}}

0 个答案:

没有答案