1

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.

5
  • Please don't suggest enabling a big heap because my application is too small? Commented Jun 20, 2021 at 7:32
  • 1
    ˋINTERGERˋ in your create statement should probably be INTEGER…? Commented Jun 20, 2021 at 7:33
  • @forpas sorry, I used your code to solve it. Now I have accepted your answer. But even that answer is not waste. check my edit. why the data is not saving in my model class. Commented Jun 20, 2021 at 11:38
  • @SachinBurdak if you have a new problem you should ask a new question and explain there what the problem is. By editing your current question with a new requirement you invalidate the answers that you already got. Commented Jun 20, 2021 at 11:41
  • @forpas done! stackoverflow.com/questions/68055617/… Commented Jun 20, 2021 at 12:01

2 Answers 2

2

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();
}
Sign up to request clarification or add additional context in comments.

Comments

1

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();
}

1 Comment

while (cursor.moveToFirst()) is not correct replace this with while (cursor.moveToNext())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.