Search code examples
androidsqliteandroid-cursor

Getting Data from Cursor


What I need to do is get the Points for a certain User in a certain Shop, I run this;

                Cursor cursor1 = myDB.checkPointss(email, name);
                String one = cursor1.getString(cursor1.getColumnIndex("POINTS"));

                scanText.setText("barcode resueeeelt " + one);

This is the Method;

public Cursor checkPointss(String email, String name) {
    Cursor c = db.rawQuery("SELECT * FROM compdata WHERE EMAIL= ? AND NAME=?", new String[] {email, name});
    if (c != null) {
        c.moveToFirst();
    }
    return c;

}

Everytime I try Login to the page, it crashes over trying to get the points? I've tried so many different codes but none seem to work.

So I just want it to find the points for the row that contains that username and shop name, then add a certain amount to the points and update the database.

02-18 20:07:42.694  15570-15570/com.adventorious.quickrewards E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.adventorious.quickrewards, PID: 15570
    java.lang.IllegalStateException: Couldn't read row 0, col -1 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:439)
            at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
            at com.adventorious.quickrewards.Scan$3.onPreviewFrame(Scan.java:155)
            at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1016)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

THIS WORKS;

public boolean checkPoints(String email, String name){
    Cursor c = db.rawQuery("SELECT POINTS FROM compdata WHERE EMAIL= ? AND NAME=?", new String[] {email, name});
    if(c.moveToFirst()) {
        return true;
    }  else {
        return false;

    }
}

So why doesn't it work to get the data?


Solution

  • getColumnIndex() is case sensitive while SQL is not.

    If the column is lowercase in the table, it will be lowercase in the SELECT * result set, and trying to retrieve it with uppercase column name won't work.