I need to fetch all data from a table,I used the following code to do the same in sqlite dbhelper class:
public Cursor getData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + PRODUCT_DETAILS_TABLE, null);
return res;
}
Now i'm fetching data using curser in my activity in the following way:
public HashMap<String, ArrayList<CartModal>> getDatas() {
map = new HashMap<String, ArrayList<CartModal>>();
stored_data = new ArrayList<CartModal>();
Cursor cartdb = dbHelper.getData();
if (cartdb != null && cartdb.getCount() > 0) {
if (cartdb.moveToFirst()) {
do {
String mloggedinuser = cartdb.getString(0);
String mname = cartdb.getString(1);
String email = cartdb.getString(2);
String mobile = cartdb.getString(3);
String about = cartdb.getString(4);
String pid = cartdb.getString(5);
String refnum = cartdb.getString(6);
String size = cartdb.getString(7);
String weight = cartdb.getString(8);
String price = cartdb.getString(9);
String catid = cartdb.getString(10);
String image = cartdb.getString(11);
CartModal cartarray = new CartModal(mloggedinuser, mname, email, mobile, about, pid, refnum, size, weight, price, catid, image);
stored_data.add(cartarray);
keys = mname;
map.put(mname, stored_data);
} while (cartdb.moveToPosition(stored_data.size()));
cartdb.close();
}
}
return map;
}
When i run the above program i'm getting error as follows:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: sdetech.io.brouchuretabproject, PID: 2434
java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at sdetech.io.brouchuretabproject.Fragments.Cart.getDatas(Cart.java:58)
at sdetech.io.brouchuretabproject.Fragments.Cart.onCreateView(Cart.java:42)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I've gone through many solutions in stackoverflow (android java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow ),but none helped me to solve this issue. Please suggest me a solution.
I was doing everything right.but the problem was,i was saving an image in db as base64 ecoded string.while fetching that data it shows illegal exception.when i removed that image it works pefectly. If anyone know how to save base64 encoded image as string in sqlite,please reply below.
Thankyou