user3818614
user3818614

Reputation: 1

ERROR -android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

Im trying to implement a score functionality in my App but I always get that same error that is in the main title, my error is in the following button

     em=bundle.getString("EMAIL");
    SQL usdbh =new SQL (this, "DBUsuarios", null, 1);
    final SQLiteDatabase db = usdbh.getWritableDatabase();
    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0)
        {
             Cursor c = db.rawQuery(" SELECT p1,p2,p3,p4,p5 FROM Usuarios WHERE Email='"+em+"' ", null);
                c.moveToFirst();
                    p1= c.getInt(0);

                      p2= c.getInt(1);

                      p3= c.getInt(2);

                     p4= c.getInt(3);

                     p5= c.getInt(4);

                     if(n7>p1){
                      db.execSQL("UPDATE Usuarios SET p1="+n7+" WHERE Email='"+em+"' ");
                      db.execSQL("UPDATE Usuarios SET p2="+p1+" WHERE Email='"+em+"' ");
                      db.execSQL("UPDATE Usuarios SET p3="+p2+" WHERE Email='"+em+"' ");
                      db.execSQL("UPDATE Usuarios SET p4="+p3+" WHERE Email='"+em+"' ");
                      db.execSQL("UPDATE Usuarios SET p5="+p4+" WHERE Email='"+em+"' ");

                     }
                     if(n7>p2&&n7<p1){
                      db.execSQL("UPDATE Usuarios SET p2="+n7+" WHERE Email='"+em+"' ");
                      db.execSQL("UPDATE Usuarios SET p3="+p2+" WHERE Email='"+em+"' ");
                      db.execSQL("UPDATE Usuarios SET p4="+p3+" WHERE Email='"+em+"' ");
                      db.execSQL("UPDATE Usuarios SET p5="+p4+" WHERE Email='"+em+"' ");

                     }
                     if(n7>p3&&n7<p2){

                      db.execSQL("UPDATE Usuarios SET p3="+n7+" WHERE Email='"+em+"' ");
                      db.execSQL("UPDATE Usuarios SET p4="+p3+" WHERE Email='"+em+"' ");
                      db.execSQL("UPDATE Usuarios SET p5="+p4+" WHERE Email='"+em+"' ");

                     }
                     if(n7>p4&&n7<p3){


                      db.execSQL("UPDATE Usuarios SET p4="+n7+" WHERE Email='"+em+"' ");
                      db.execSQL("UPDATE Usuarios SET p5="+p4+" WHERE Email='"+em+"' ");

                     }
                     if(n7>p5&&n7<p4){


                      db.execSQL("UPDATE Usuarios SET p5="+n7+" WHERE Email='"+em+"' ");

                     }

             c.close();
                Intent intent2 = new Intent(Cinco.this, MainActivity.class);
                    Bundle b = new Bundle();
                    b.putInt("PUNT", n7);
                    intent2.putExtras(b);
                    startActivity(intent2);
        }
    });

I did the same cursor the same at the MainActivity class with no errors but it just putted the Scores in textViews it didnt insert values into the tables

The scores are inserted correctly but I always get" Unfortunately, MyApp has stopped." =( Any ideas? PD= this is my first question so be pattient if I did something wrong =)

Upvotes: 0

Views: 332

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93569

First off, always post the entire stack trace.

Secondly, you tried to access a cursor with no results. This caused you to crash. Check the return value of moveToFirst. If it returns false, you have no results and need to not access it.

Additionally, NEVER write an SQL query with concatenation to make the WHERE clause. Always use bind variables. Using concatenation causes you to risk SQL injection and it prevents the db from optimizing for the generalized query.

Upvotes: 1

Related Questions