Java.lang.OutOfMemoryError: [memory Exhausted] While Reading Data From Sqlite Android
I have successfully saved my data in SQLite DB. But I am getting an error while reading data from my SQLite DB and then my app is crashing. error message 01-01 05:42:13.916 607-6
Solution 1:
Your loop never ends because in each iteration you set the cursor's index at the 1st row with moveToFirst() without advancing to the next row.
Use moveToNext() only:
public void ReadSqliteData(Context context){
ArrayList<Model> list = new ArrayList<>();
Adpter adpter = new Adpter(list,context);
SQLiteDatabase database = getWritableDatabase();
Cursor cursor = database.rawQuery("Select name, image from orders",null);
while (cursor.moveToNext()){
Model model = new Model();
model.setImage(cursor.getString(0));
model.setName(cursor.getString(1));
list.add(model);
}
adpter.notifyDataSetChanged();
cursor.close();
database.close();
}
Solution 2:
Try with the following code.
//update your table create query, id should be auto increment and also your query was not in correct form.
db.execSQL(
"Create Table orders"+
"("+ id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"image text ,"+
"name text" +")"
);
//update you read method code
public void ReadSqliteData(Context context){
ArrayList<Model> list = new ArrayList<>();
Adpter adpter = new Adpter(list,context);
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database.rawQuery("Select name, image from orders",null);
if (cursor.moveToNext()){
while (cursor.moveToFirst()){
Model model = new Model();
model.setImage(cursor.getString(0));
model.setName(cursor.getString(1));
list.add(model);
}
adpter.notifyDataSetChanged();
}
cursor.close();
database.close();
}
Post a Comment for "Java.lang.OutOfMemoryError: [memory Exhausted] While Reading Data From Sqlite Android"