0

I'm trying to retrieve data from sqlite data base but but when I call the nameData() logcat shows the exception: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 I don't understand why, any clues?

process:

    public class SearchContactByName2 extends AppCompatActivity {
    String dbString="",dbString2="";
        SQLiteDatabase db;
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.searchcontactbynamelayout2_main);

            TextView textView=(TextView)findViewById(R.id.textViewShowName);
            TextView textView2=(TextView)findViewById(R.id.textView2ShowNumber);

            SearchContactByName objOfSearchContactByName=new SearchContactByName();

            ContactDatabase onbOfContactDatabase=new ContactDatabase(getBaseContext());

            Cursor allcontact2= onbOfContactDatabase.nameData(objOfSearchContactByName.getNameForSearchTypeString);


            allcontact2.moveToFirst();

            do{
                dbString+=allcontact2.getString(allcontact2.getColumnIndex("name"));
                dbString2+=allcontact2.getString(allcontact2.getColumnIndex("phone"));
                dbString+="\n";
                dbString2+="\n";
                textView.setText(dbString);
                textView2.setText(dbString2);
            }while(allcontact2.moveToNext());
            Toast.makeText(getBaseContext(), "data", Toast.LENGTH_LONG).show();

        }
    }

database part:

public class ContactDatabase extends SQLiteOpenHelper {
    SQLiteDatabase db;
    public static final String DATABASE_NAME="totalContact.db";
    public static final  String TABLE_NAME="mecontact";
    public static final  String NAME="name";
    public static final  String PHONE="phone";

    public ContactDatabase(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL("create table mecontact" +
                    "(id integer primary key autoincrement, name text, phone text)");
        }catch(android.database.SQLException e){
                System.out.println("table create nhi ho rha");
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS mecontact");
        onCreate(db);
    }

    public void insertContact(String nam,String mob){

        db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();

        contentValues.put(NAME,nam);
        contentValues.put(PHONE,mob);

        db.insert(TABLE_NAME, null, contentValues);
        db.close();
    }

    public Cursor showData(){

        db=this.getWritableDatabase();
        Cursor res =  db.rawQuery("SELECT  * FROM mecontact", null);
        return res;

    }

    public Cursor nameData(String dataName){

        db=this.getWritableDatabase();
        Cursor res =  db.rawQuery("SELECT * FROM mecontact WHERE name = '"+dataName+"'", null);
        return res;

    }
}
Matthias Kricke
  • 4,931
  • 4
  • 29
  • 43
xyz rety
  • 671
  • 2
  • 11
  • 22
  • First compare your code with this link https://stackoverflow.com/questions/24901617/insert-values-in-to-sqlite-database-java-lang-nullpointerexception/24901693#24901693 and then you will find that where you have wrong – DJhon Jun 10 '16 at 09:42
  • once look at this you will get a idea http://androidcoding.in/2016/03/22/android-sqlite-tutorial-on-inserting-deleting-values-into-database/ – Abhishek Jun 10 '16 at 15:27

4 Answers4

0

try below code

 if(allcontact2.moveToFirst()){

      do{
            dbString+=allcontact2.getString(allcontact2.getColumnIndex("name"));
            dbString2+=allcontact2.getString(allcontact2.getColumnIndex("phone"));
            dbString+="\n";
            dbString2+="\n";
            textView.setText(dbString);
            textView2.setText(dbString2);
        }while(allcontact2.moveToNext());}
        Toast.makeText(getBaseContext(), " no data", Toast.LENGTH_LONG).show();

actually your database has no data

Sush
  • 3,864
  • 2
  • 17
  • 35
0
Cursor allcontact2= onbOfContactDatabase.nameData(objOfSearchContactByName.getNameForSearchTypeString);

if(allcontact2.size() > 0){
while(allcontact2.moveToNext()){
   dbString+=allcontact2.getString(allcontact2.getColumnIndex("name"));
   dbString2+=allcontact2.getString(allcontact2.getColumnIndex("phone"));
   dbString+="\n";
   dbString2+="\n";
   textView.setText(dbString);
   textView2.setText(dbString2);
  }
  Toast.makeText(getBaseContext(), "data", Toast.LENGTH_LONG).show();
}
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

Along with the answer by sush change nameData method to,

public Cursor nameData(String dataName){
    db=this.getReadableDatabase();
    //String dataname might contain special characters like a quote
    //retreive the cursor this way
    Cursor res=db.query("mecontact",new String[]{columnsYouwantToSelect},"name =?",new String[]{dataName},null,null,null);
    //if you want to select all the columns in the table replace
    //the second parameter with null
    return res;
}
Community
  • 1
  • 1
Malith Lakshan
  • 762
  • 6
  • 12
0

Not really answering your question, but more advice on how to prevent trouble in the future. I would suggest you change your oncreate code to this and declare your ID value in the top like you did for the others. This will make sure the database is created correctly and that in the future if changes happen you can easily get values without making typing errors. Code like this is safer to use than pure queries.

db.execSQL("CREATE TABLE "
            + TABLE_NAME
            + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + NAME + " TEXT,"
            + PHONE + " TEXT);" );
skultech
  • 26
  • 3