Question

I am trying read database from external SDCard It Works For Me But when i get the value From the cursor it throws error IndexOutOfBoundException

MyCode is

public class Database{

    public static final String  DATABASE_NAME = "Test/EduTab.s3db";

    private SQLiteDatabase database;
    private File SDCardDir = Environment.getExternalStorageDirectory();

    public Database()
    {
        try
        {
            File file = new File(SDCardDir + File.separator + DATABASE_NAME);
            if(file.exists()){
                database = SQLiteDatabase.openDatabase(SDCardDir + File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READONLY);  
            }
        }
        catch (SQLiteException ex)
        {
            ex.printStackTrace();
        }
    }

    public SQLiteDatabase getdatabase(){
        return database;
    }
}

Main Activity

public class MainActivity extends Activity {

    SQLiteDatabase mDatabase;
    Database mData = new Database();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDatabase = mData.getdatabase();

        Cursor mCursor = mDatabase.rawQuery("Select * From mst_Subject",null);
        String[] ColumnNames = mCursor.getColumnNames();

        Log.d("Cursor Size",""+mCursor.getCount()); // It Works Returns Size of The  Table 

        for(int row=0;row<mCursor.getCount();row++)
        {
            for(int col=0;col<ColumnNames.length;col++){

                Log.d("ColumnName",""+ColumnNames[col]); // It Works Returns The Name Of The Column
                Log.d("Column Value",mCursor.getString(mCursor.getColumnIndex(ColumnNames[col])));  // Not Working It Throw Error           

                /*
                 * Error Is 
                 * java.lang.RuntimeException: Unable to start activity
                 * ComponentInfo{com.example.readdatabasefromsdcard/com.example.
                 * readdatabasefromsdcard.MainActivity}:
                 * android.database.CursorIndexOutOfBoundsException: Index -1
                 * requested, with a size of 8
                 */

                Log.d("Record["+row+"]["+col+"]",mCursor.getString(mCursor.getColumnIndex(ColumnNames[col])));

            }
            Log.d("End Of The Record ["+row+"]","==========================================================");
        }

    }
}
Was it helpful?

Solution

Just write following statement before the for loop

mCursor.moveToFirst();

OTHER TIPS

Change

for(int row=0;row<mCursor.getCount();row++)

to

 int row = -1;
 while (mCursor.moveToNext()) {
     row++;
     // now use cursor.getString()

A Cursor is initially positioned before the first row, there is nothing you can read there

Use This Code:

    if(mCursor.moveToFirst())
    {
        do
        {

        }while(mCursor.moveToNext());
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top