Search code examples
javaandroidsqliteandroid-cursor

Index 0 Requested with a size of 0 Error in SQLITE DB


There is data in the database (2 rows to be precise) with info in each.

Here is the code (that matters, not all of it) from the DBADAPTER:

// Field Names:
    public static final String KEY_ROWID = "_id";
    public static final String KEY_DEVICE = "device";
    public static final String KEY_TYPE = "type";
    public static final String KEY_DEVID = "devid";

    public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_DEVICE, KEY_TYPE, KEY_DEVID};
    public static final String[] DEVID_KEY = new String[] {KEY_DEVID};

    // Column Numbers for each Field Name:
    public static final int COL_ROWID = 0;
    public static final int COL_DEVICE = 1;
    public static final int COL_TYPE = 2;
    public static final int COL_DEVID = 3;

    // DataBase info:
    public static final String DATABASE_NAME = "dbDevices";
    public static final String DATABASE_TABLE = "mainDevices";
    public static final int DATABASE_VERSION = 10; // The version number must be incremented each time a change to DB structure occurs.

    //SQL statement to create database
    private static final String DATABASE_CREATE_SQL = 
            "CREATE TABLE " + DATABASE_TABLE 
            + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_DEVICE + " TEXT NOT NULL, "
            + KEY_TYPE + " TEXT, "
            + KEY_DEVID + " TEXT NOT NULL "
            + ");";

    private final Context context;
    private DatabaseHelper myDBHelper;
    private SQLiteDatabase db;

    // Get a specific row (by devid)
        public String getDevName(String devid) {
            String name;
            String where = KEY_DEVID + " like '%" + devid + "%'";
            Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                            where, null, null, null, null, null);
                c.moveToFirst();
                name = c.getString(COL_DEVICE);
            return name;
        }

Here is the code that calls it:

String incomingMessage = " ";
incomingMessage = in.readLine() + System.getProperty("line.separator");
devName = devDB.getDevName(incomingMessage);

And I know that this is getting it info because it logs:
03-16 15:39:47.072: V/String(18073): 5122

Whenever I run this I get the error:

03-16 15:39:47.122: W/System.err(18073): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
03-16 15:39:47.122: W/System.err(18073):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
03-16 15:39:47.122: W/System.err(18073):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
03-16 15:39:47.132: W/System.err(18073):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
03-16 15:39:47.132: W/System.err(18073):    at com.ti.cc3x.android.DBAdapter.getDevName(DBAdapter.java:121)
03-16 15:39:47.132: W/System.err(18073):    at com.ti.cc3x.android.broadcastListener$1.run(broadcastListener.java:112)
03-16 15:39:47.132: W/System.err(18073):    at java.lang.Thread.run(Thread.java:841)

I have tried checking for not null cursor. All that does is tell me nothing. I know there is data where I am asking it to look I just do not know why it will not show it. Can anyone tell me where I went wrong? Thank you!


Solution

  • Expanding on the answer from @bwegs regarding the call to getWritableDatabase().

    Try using the selectionArgs since it's cleaner. Check the return value of moveToFirst(), and be sure to close your cursor to avoid memory leaks.

     public String getDevName(String devid) {
        db = myDBHelper.getWritableDatabase();  //get db if not done so already, you may have already done this in code not shown
    
        String name = null;
        String where = KEY_DEVID + " like ?";
        String[] selectionArgs =  new String[] {"%"+ devid + "%" };
        Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, selectionArgs , null, null, null, null);
    
        //Only use cursor if moveToFirst() succeeds
        //this will make it so that you don't get the CursorIndexOutOfBoundsException
        if (c.moveToFirst()){
            name = c.getString(COL_DEVICE);
        }
    
        c.close(); //close your cursor to avoid memory leak!
    
        db.close(); //close here if you call getWritableDatabase() in beginning of this function
    
        //This will return null if no results from query
        return name;
    }