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-607/com.example.eyesaver I/dalvikvm: | sysTid=607 nice=0 sched=0/0 cgrp=apps handle=68313184
01-01 05:42:13.916 607-607/com.example.eyesaver I/dalvikvm: | sysTid=607 nice=0 sched=0/0 cgrp=apps handle=68313184
01-01 05:42:13.916 607-607/com.example.eyesaver I/dalvikvm: | state=R schedstat=( 0 0 0 ) utm=2910 stm=2910 core=0
01-01 05:42:13.916 607-607/com.example.eyesaver I/dalvikvm: | state=R schedstat=( 0 0 0 ) utm=2910 stm=2910 core=0
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm: at android.database.CursorWindow.nativeGetString(Native Method)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm: at android.database.CursorWindow.nativeGetString(Native Method)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm: at android.database.CursorWindow.getString(CursorWindow.java:434)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm: at android.database.CursorWindow.getString(CursorWindow.java:434)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm: at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm: at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm: at com.example.eyesaver.SQ_Lite_DB.ReadSqliteData(SQ_Lite_DB.java:59)
01-01 05:42:13.917 607-607/com.example.eyesaver I/dalvikvm: at com.example.eyesaver.SQ_Lite_DB.ReadSqliteData(SQ_Lite_DB.java:59)// I marked this line in code
and
1-01 05:42:16.189 607-607/com.example.eyesaver E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.eyesaver, PID: 607
java.lang.OutOfMemoryError: [memory exhausted]
at dalvik.system.NativeStart.main(Native Method)
01-01 05:42:16.189 607-607/com.example.eyesaver E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.eyesaver, PID: 607
java.lang.OutOfMemoryError: [memory exhausted]
at dalvik.system.NativeStart.main(Native Method)
I am reading data from my SQLite using the 'ReadSqliteData()' method
ReadSqliteData() method
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);
if (cursor.moveToFirst()){
while (cursor.moveToFirst()){
Model model = new Model();
model.setImage(cursor.getString(0)); //i am getting error here
model.setName(cursor.getString(1)); //i am getting error here
list.add(model);
}
adpter.notifyDataSetChanged();
}
cursor.close();
database.close();
}
And one more problem: my id is null in the SQLite database, I don't know why? I am creating a table like this
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"Create Table orders"+
"(id INTERGER PRIMARY KEY,"+
"image text ,"+
"name text)"
);
}
If you need any other information let me know.
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();
}