error in db.query and its method

February 10, 2018, at 04:20 AM

I'm trying to read 2nd column in last record using a cursor. First, does the code layout for query and method looks workable? If not what could be improved?

Second, I lack experience to detect the error and would appreciate suggestion to correct the code.

from Class:

SQLiteDatabase db = this.getWritableDatabase();
try {
                Cursor cursor = this.getSurveyData();
                        String name = cursor.getString(2);
            }catch (SQLiteException e )
                                            Log.v(TAG, "Exception " + e.getMessage());


    public Cursor getSurveyData(){
      SQLiteDatabase db = this.getWritableDatabase();
    String[] projection = {
         // String sortOrder = "ID1" + " DESC";
      return db.query(

Error from debug :

E/AndroidRuntime: FATAL EXCEPTION: main
              Process:, PID: 4092
              java.lang.IllegalStateException: Couldn't read row 6, col 2 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(
                  at android.database.AbstractWindowedCursor.getString(
                  at android.view.View.performClick(
                  at android.view.View$
                  at android.os.Handler.handleCallback(
                  at android.os.Handler.dispatchMessage(
                  at android.os.Looper.loop(
                  at java.lang.reflect.Method.invoke(Native Method)

Disconnected from the target VM, address: 'localhost:8603', transport: 'socket'

Thanks everyone, I rewrote it a bit, works now.Next is to compare dates to see if to append or insert in database. Thats another story.

String dd1="",dbdato="";
            SQLiteDatabase db = this.getWritableDatabase();
            long millis1=System.currentTimeMillis();
            final Date dat1=new java.sql.Date(millis1);
            dd1 = dat1.toString();
            Cursor cursor = db.query(table1,new String[]{COLUMN_DATO1},null,null,null,null,null);
            if (cursor.moveToLast()) {
            dbdato = cursor.getString(cursor.getColumnIndex(COLUMN_DATO1));
Answer 1

When using Cursor get???? methods (e.g. getString(?)), the parameter is the offset as opposed to the column order. Thus the first column is offset 0, the 2nd column is offset 1.

It is generally better/more flexible to not use hard coded offsets but to use getColumnIndex(column_name) to get the index.

So from your description as wanting the 2nd column then use:-

String name = cursor.getString(1);

If, as an example, your 2nd column is named name then using :-

String name = cursor.getString(cursor.getColumnIndex("name"));

would be less problematic and more flexible.

Answer 2

Your projection only contains one column, you need to add all you want to fech:

 String[] projection = {
Image from gallery is not displayed on the image button

Image from gallery is not displayed on the image button

From the App I should be able to add an image from my gallery to the image button, then add Food name, Description and Price and finally click uploadThe image should be uploaded to my firebase storage while Price,Description and Food name should be uploaded...

Make ImageView to look the same on different devices

Make ImageView to look the same on different devices

I have a RelativeLayout with an ImageView that has the following properties,

compile gradle task is always executed

compile gradle task is always executed

I have a multidex project with minSDK 21, using gradle 45

Not sure why componentWillMount is not updating state

Not sure why componentWillMount is not updating state

I am using following code to fetch and render data from the published API source but unable to set the state after successfully getting the data on the consoleFollowing is my code;